小白题解 Codeforces 798B Mike and strings
2017-05-14 23:50
337 查看
(模拟题,暴力求解) 题目链接:点击打开链接
B. Mike and strings
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Mike has n strings
s1, s2, ..., sn each consisting of lowercase English letters. In one move he can choose a stringsi,
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. Thei-th line corresponding to string
si. Lengths of strings are equal. Lengths of each string is positive and don't exceed50.
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.
Examples
Input
Output
Input
Output
Input
Output
Input
Output
Note
In the first sample testcase the optimal scenario is to perform operations in such a way as to transform all strings into "zwoxz".
题目大意:给出一组长度相同的字符串,每次操作可以将一个字符串的首元素放到末尾,要使各字符串完全一致,需要多少次操作。
分析:以第一个字符串顺序为依照,依次枚举最终字符串以s[0][i]开头的情况,对每种情况去分别计算每个字符串需要的操作数并求和,最终再找到cnt[i]最小值即为答案。复杂度是n^4,可过。
下面是AC代码(用来好长时间debug)
B. Mike and strings
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
Mike has n strings
s1, s2, ..., sn each consisting of lowercase English letters. In one move he can choose a stringsi,
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. Thei-th line corresponding to string
si. Lengths of strings are equal. Lengths of each string is positive and don't exceed50.
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.
Examples
Input
4 xzzwo zwoxz zzwox xzzwo
Output
5
Input
2 molzv lzvmo
Output
2
Input
3 kc kc kc
Output
0
Input
3 aa aa ab
Output
-1
Note
In the first sample testcase the optimal scenario is to perform operations in such a way as to transform all strings into "zwoxz".
题目大意:给出一组长度相同的字符串,每次操作可以将一个字符串的首元素放到末尾,要使各字符串完全一致,需要多少次操作。
分析:以第一个字符串顺序为依照,依次枚举最终字符串以s[0][i]开头的情况,对每种情况去分别计算每个字符串需要的操作数并求和,最终再找到cnt[i]最小值即为答案。复杂度是n^4,可过。
下面是AC代码(用来好长时间debug)
#include<cstdio> #include<cstring> const int n=50; char s[n+5][n+5]; int main() { int n; scanf("%d",&n); for(int i=0;i<n;i++) { scanf("%s",s[i]); } int len=strlen(s[0]); int cnt[len+5]; for(int i=0;i<len+5;i++) { cnt[i]=0; } int flag=1; for(int i=0;i<len&&flag==1;i++) //若flag为0说明上一轮不匹配,则可退出了 { for(int j=0;j<n&&flag==1;j++) //若flag为0说明上一轮不匹配,则可退出了 { flag=0; //重置为没找到,为下一个循环使用 for(int k=0;k<len&&!flag;k++) //(!flag)是非常重要的条件,不要丢掉 { if(s[j][k]==s[0][i]) { int is=i; //不要放错了位置(原则:紧跟作用域) int r; for(r=k+1;r<k+len;r++) if(s[j][r%len]!=s[0][(++is)%len]) { break; } if(r==(k+len)) { flag=1; cnt[i]+=k; } } } } } if(flag) { int minn=cnt[0]; for(int i=1;i<len;i++) { if(cnt[i]<minn) minn=cnt[i]; } printf("%d\n",minn); } else printf("-1\n"); return 0; }
相关文章推荐
- 小白题解 Codeforces 794A Bank Robbery
- [置顶] 小白题解 Codeforces 794B Cutting Carrot
- 小白题解 Codeforces 807A Is it rated?
- 小白题解 Codeforces 805B 3-palindrome
- 小白题解 Codeforces 807B T-Shirt Hunt
- 小白题解 Codeforces 804A Find Amir
- 小白题解 Codeforces 1A Theatre Square
- 小白题解 Codeforces 785A
- 小白题解 Codeforces 785B Anton and Classes
- 小白题解 Codeforces 266A Stones on the Table
- 小白题解 Codeforces 798A Mike and palindrome
- Codeforces 95C Volleyball 题解
- Codeforces 813C The Tag Game 题解
- 题解-python-CodeForces 227A
- Codeforces Round #403 div2 (CodeForces - 782) 题解
- Codeforces 876A Trip For Meal 题解
- 【组合】Codeforces40E[Number Table]题解
- 7_6_R题 Bad Luck Island题解[Codeforces 540D](概率DP)
- Codeforces 935C Fifa and Fafa 题解 附平面几何基础:平面向量基本运算
- Codeforces 115A Party 题解