Mike and palindrome
time limit per test 2 second memory limit per test 256 megabytes
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?

传送门:CF798B

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

1
2
3
4
5
4
xzzwo
zwoxz
zzwox
xzzwo

Sample Output

1
5

题解

暴力就可以了,遍历一遍求所有其他字符串到自己字符串的操作数,求最小就可以了,全部变不到就是-1
## AC code:(不包含输入类)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
import java.io.*;  
import java.util.*;
public class Main {

public static void main(String[] args) {
FastScanner sc=new FastScanner();
PrintWriter pw=new PrintWriter(System.out);
while(sc.hasNext()){
int n=sc.nextInt();
String[]s=new String[n];
int[][]jl=new int[n][27];
for(int i=0;i<n;i++){
s[i]=sc.next();
for(int j=0;j<s[i].length();j++){
jl[i][s[i].charAt(j)-'a']++;
}
}
boolean flag=true;
for(int i=1;i<n;i++){
for(int j=0;j<26;j++){
if(jl[i][j]!=jl[0][j]){
flag=false;
break;
}
}
}
if(!flag){
pw.println(-1);
}else if(n==1){
pw.println(0);
}else{
int min=99999999;
for(int i=0;i<n;i++){
boolean f=true;
int sum=0;
for(int j=0;j<n;j++){
if(j!=i){
int re=getdif(s[i],s[j]);
if(re==99999999){
f=false;
break;
}
sum+=re;
}
}
if(f) min=Math.min(min, sum);
}
if(min==99999999)pw.println(-1);
else pw.println(min);

}
pw.flush();
}
}
static int getdif(String s,String e){
if(s.equals(e))return 0;
else{
int min=99999999;
StringBuilder sb=new StringBuilder();
sb.append(s);
sb.append(s);
for(int i=0;i<sb.length()-e.length();i++){
int j;
for(j=0;j<e.length();j++){
if(e.charAt(j)!=sb.charAt(i+j)){
break;
}
}
if(j==e.length())min=Math.min(min, Math.abs(i-j));
}
return min;
}
}
}