D - Mike and strings
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.
Example
4 xzzwo zwoxz zzwox xzzwo
5
2 molzv lzvmo
2
3 kc kc kc
0
3 aa aa ab
-1 题意:输入一个整数n,然后输入n个长度相同的字符串,如果能通过循环位移使这n个字符串都相等,输出最小的操作次数,如果不能,输出-1; 题解:1.首先判断能不能通过循环位移使这n个字符串都相等 2.计算把第第1,2。。。n行字符串当模板使所有字符串都相等的操作次数 3.输出最小的值。 代码:
#include#include #include #include #include using namespace std;int main(){ string str[55]; int i,j,n,ans[55],k,book=0; int x[55],b; cin>>n;string T,W; memset(x,0,sizeof(x)); memset(ans,0,sizeof(ans)); for(i=1;i<=n;i++) { cin>>str[i]; } for(i=0;i