Codeforces Round #410 (Div. 2)B. Mike and strings(暴力)
Description
Mike has n strings s1, s2, ..., sn each consisting of lowercase English letters. In one move he can choose a string si, erase the first character and append it to the end of the string. For example, if he has the string "coolmike", in one move he can transform it into the string "oolmikec".
Now Mike asks himself: what is minimal number of moves that he needs to do in order to make all the strings equal?
Input
The first line contains integer n (1 ≤ n ≤ 50) — the number of strings.
This is followed by n lines which contain a string each. The i-th line corresponding to string si. Lengths of strings are equal. Lengths of each string is positive and don't exceed 50.
Output
Print the minimal number of moves Mike needs in order to make all the strings equal or print - 1 if there is no solution.
Sample Input
4
xzzwo
zwoxz
zzwox
xzzwo
2
molzv
lzvmo
3
kc
kc
kc
3
kc
kc
kc
Sample Output
5
2
0
-1
思路
题解:
数据范围很小,因此直接暴力求解。
#include<bits/stdc++.h> using namespace std; const int INF = (1<<30)-1; const int maxn = 55; char str[maxn][maxn]; char tmp[maxn],tmp1[maxn]; int main() { //freopen("input.txt","r",stdin); int n,res = INF; bool flag = false,success = true; scanf("%d",&n); for (int i = 0;i < n;i++) { scanf("%s",str[i]); } int len = strlen(str[0]); for (int i = 0;i < n;i++) { int cnt = 0; for (int j = 0;j < n;j++) { if (i == j) { flag = true; continue; } flag = false; if (strcmp(str[j],str[i]) == 0) { flag = true; continue; } strcpy(tmp1,str[j]); int tmplen = len; while (--tmplen) { strncpy(tmp,tmp1+1,len-1); tmp[len - 1] = tmp1[0]; cnt++; strcpy(tmp1,tmp); if (strcmp(tmp1,str[i]) == 0) { flag = true; break; } } if (!flag) { success = false; break; } } if (!flag) { success = false; break; } res = min(res,cnt); } if (!success) printf("-1\n"); else printf("%d\n",res); return 0; }
- Codeforces Round #410 Div2 D Mike and distribution
- Codeforces Round #410 (Div. 2) Mike and gcd problem 思维
- Codeforces Round #305 (Div. 2) A. Mike and Fax 暴力回文串
- Codeforces Round #410 (Div. 2) D. Mike and distribution(贪心)
- Codeforces Round #305 (Div. 1) A. Mike and Frog 暴力
- Mike and gcd problem Codeforces Round #410 (Div. 2)
- Mike and strings-codeforces-Round 410-Div2-B
- Codeforces Round #410 (Div.2) D.Mike and distrubition-构造
- Codeforces Round #293 (Div. 2) -- A. Vitaly and Strings (字符串构造)
- Codeforces Round #361 (Div. 2)E. Mike and Geometry Problem
- Codeforces Round #361 (Div. 2) C. Mike and Chocolate Thieves
- Codeforces Round #361 (Div. 2)B. Mike and Shortcuts【BFS】
- codeforces B. Mike and strings 暴力+字符串
- Codeforces Round #305 (Div. 2) D. Mike and Feet 单调栈
- Codeforces Round #293 (Div. 2) A. Vitaly and Strings(思路)
- Codeforces Round #401 (Div. 2) C. Alyona and Spreadsheet(暴力)
- Codeforces Round #305 (Div. 1) B. Mike and Feet(思路)
- 【Codeforces Round 272 (Div 2)C】【暴力】Dreamoon and Sums 所有除b结果是模b结果[1,a]倍数字之和
- Codeforces Round #272 (Div. 2) B. Dreamoon and WiFi 暴力
- 暴力/set Codeforces Round #291 (Div. 2) C. Watto and Mechanism