CodeForces - 798B:Mike and strings
5232 단어 [기록] 알고리즘 문제풀이
출처:
태그:
참조 자료:
비슷한 제목:
제목.
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?
입력
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.
출력
Print the minimal number of moves Mike needs in order to make all the strings equal or print - 1 if there is no solution.
프롬프트
In the first sample testcase the optimal scenario is to perform operations in such a way as to transform all strings into “zwoxz”.
샘플 1 입력
4 xzzwo zwoxz zzwox xzzwo
출력 예제 1
5
샘플 2 입력
2 molzv lzvmo
출력 예제 2
2
샘플 3 입력
3 kc kc kc
출력 예제 3
0
샘플 4 가져오기
3 aa aa ab
출력 예제 4
-1
문제풀이의 방향
참조 코드
#include
#include
int main()
{
int n;//
scanf("%d",&n);
char str[n][55];
int i,j,k,c;
for(i=0;i"%s",str[i]);//
int len=strlen(str[0]);//
int min;//
int judge=1;//
for(i=0;i// str[i]
{
int sum=0;//
for(j=0;jif(i==j)continue;
int count=0;// ,
char temp[len+1];// ,
strcpy(temp,str[j]);
temp[len]='\0';
for(k=0;kif(str[i][k]!=temp[k])
{
count++;
// :
temp[len]=temp[0];
for(c=0;c1;c++)
temp[c]=temp[c+1];
temp[len]='\0';
k=-1;//
}
if(count==len)// , ,
{
judge=0;
goto part1;
}
}
sum+=count;
}
if(i==0)min=sum;
else if(sumsum;
}
part1:
if(judge==0)
printf("-1
");
else printf("%d
",min);
return 0;
}