链接:http://codeforces.com/contest/798
考虑不全面wa一次,水题。
1 #include<cstdio> 2 #include<cstring> 3 char s[20]; 4 5 int main() 6 { 7 scanf("%s",s); 8 int n=strlen(s); 9 int ct=0; 10 int m=n/2; 11 12 for(int i=0;i<m;i++) 13 { 14 if(s[i]!=s[n-i-1]) ct++; 15 if(ct>1) 16 { 17 puts("NO"); 18 return 0; 19 } 20 } 21 if(ct==0&&n%2==0) 22 { 23 puts("NO"); 24 return 0; 25 } 26 puts("YES"); 27 return 0; 28 29 }
数据小,我暴力过的。。。
1 #include<cstdio> 2 #include<cstring> 3 char s[50][120]; 4 5 6 int main() 7 { 8 int n; 9 scanf("%d",&n); 10 for(int i=0;i<n;i++) 11 scanf("%s",s[i]); 12 int len=strlen(s[0]); 13 int ans=0x3f3f3f3f; 14 for(int i=0;i<n;i++) 15 { 16 int temp=0; 17 for(int j=0;j<n;j++) if(i!=j) 18 { 19 for(int k=len;k<=2*len;k++) 20 s[j][k]=s[j][k-len]; 21 int d=strstr(s[j],s[i])-s[j]; 22 if(d<0||d>50) { 23 puts("-1"); 24 return 0; 25 } 26 s[j][len]='\0'; // 27 temp+=d; 28 } 29 ans=ans>temp?temp:ans; 30 } 31 printf("%d\n",ans); 32 33 }
参考:
用dp做
dp[i][j]表示使得 前i个串 和第i个串移动j次之后都相同 的 最少移动的次数
1 # include <bits/stdc++.h> 2 using namespace std; 3 # define fi cin 4 # define fo cout 5 string s[512]; 6 int dp[512][512]; 7 int main(void) 8 { 9 int n; 10 fi>>n; 11 for (int i = 1;i <= n;++i) 12 fi>>s[i]; 13 int k = s[1].length(); 14 for (int i = 1;i <= n;++i) 15 for (int j = 0;j < k;++j) 16 dp[i][j] = 1e9; 17 for (int i = 1;i <= n;++i) 18 s[i] = s[i] + s[i]; 19 for (int i = 0;i < k;++i) 20 dp[1][i] = i; 21 for (int i = 2;i <= n;++i) 22 { 23 for (int j = 0;j < k;++j) 24 for (int prev = 0;prev < k;++prev) 25 if (s[i].substr(j,k) == s[i-1].substr(prev,k)) 26 dp[i][j] = min(dp[i][j],dp[i-1][prev] + j); 27 } 28 int ans = 1e9; 29 for (int i = 0;i < k;++i) 30 ans = min(ans,dp[n][i]); 31 if (ans == 1e9) 32 puts("-1"); 33 else 34 fo << ans << '\n'; 35 return 0; 36 }
有点巧妙,想不出
代码也很简洁
1 # include <bits/stdc++.h> 2 using namespace std; 3 # define fi cin 4 # define fo cout 5 int main(void) 6 { 7 int n; 8 fi>>n; 9 int g = 0,v,cnt = 0,ans = 0; 10 while (n --) 11 { 12 int v; 13 fi>>v; 14 g = __gcd(g,v); 15 if (v & 1) ++cnt; 16 else ans += (cnt / 2) + 2 * (cnt & 1),cnt = 0; 17 } 18 ans += (cnt / 2) + 2 * (cnt & 1); 19 fo << "YES\n"; 20 if (g == 1) 21 fo << ans << '\n'; 22 else 23 fo << "0\n"; 24 cerr << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n'; 25 return 0; 26 }
慢慢要学会自己分析-_-||
1 #include<cstdio> 2 #include<cstring> 3 #include<algorithm> 4 #include<vector> 5 using namespace std; 6 const int maxn=1e5+10; 7 #define pii pair<int,int> 8 #define se second 9 #define fi first 10 #define pb push_back 11 12 pii a[maxn]; 13 int b[maxn]; 14 15 int main() 16 { 17 int n,x; 18 scanf("%d",&n); 19 for(int i=1;i<=n;i++) 20 { 21 scanf("%d",&x); 22 a[i]=pii(-x,i); //为了从大到小排序(-x,i) 23 } 24 sort(a+1,a+n+1); 25 for(int i=1;i<=n;i++) 26 scanf("%d",&b[i]); 27 vector<int> v; 28 v.pb(a[1].se); 29 for(int i=2;i<=n;i+=2) 30 { 31 if(i+1<=n&&b[a[i+1].se]>b[a[i].se]) 32 v.pb(a[i+1].se); 33 else v.pb(a[i].se); 34 } 35 int len=v.size(); 36 printf("%d\n",len); 37 for(int i=0;i<len-1;i++) 38 printf("%d ",v[i]); 39 printf("%d\n",v[len-1]); 40 }
E:Mike and code of a permutation
貌似有点难,貌似用到线段树和拓扑排序。
等有中文题解了再补吧。。。