[CF798B] Mike and strings(暴力)
题目链接:http://codeforces.com/contest/798/problem/B
题意:n个字符串,第一个字符可以放到最后,问最少移动步骤。
直接暴力O(n*n*len*len)。
1 #include <bits/stdc++.h> 2 using namespace std; 3 4 const int maxn = 55; 5 const int inf = 0x7f7f7f7f; 6 int n; 7 string s[maxn]; 8 9 int main() { 10 // freopen("in", "r", stdin); 11 while(~scanf("%d", &n)) { 12 int ret = inf; 13 for(int i = 0; i < n; i++) cin >> s[i]; 14 int len = s[0].length(); 15 for(int i = 0; i < n; i++) { 16 int tmp = 0; 17 bool exflag = 0; 18 for(int j = 0; j < n; j++) { 19 if(i == j) continue; 20 bool ok = 0; 21 for(int st = 0; st < len; st++) { 22 string t = ""; 23 for(int k = 0; k < len; k++) t += s[j][(st+k)%len]; 24 if(t == s[i]) { 25 ok = 1; 26 tmp += st; 27 break; 28 } 29 } 30 if(!ok) exflag = 1; 31 } 32 if(exflag) continue; 33 ret = min(ret, tmp); 34 } 35 printf("%d\n", ret == inf ? -1 : ret); 36 } 37 return 0; 38 }