题意:
给出 N 个串,问最少能移动几次使 N 个串都相等。
移动的规则是,每次只能将一个串首的字符移到串尾。
N 最大 50,串长最大 50。
思路:
竟然是暴力题。 50^4 并不超时……
代码:
#include <iostream>
#include <cstring>
#include <map>
#include <string>
#include <cstdio>
using namespace std;
string str[51];
int n;
int tans,ans,fans,len;
int judge(int id){
bool flag;
for(int i=0;i<len;i++){
flag=1;
for(int j=0;j<len&&flag;j++){
if(str[0][j]!=str[id][(i+j)%len])
flag=0;
}
if(flag)
return i;
}
return -1;
}
int solve(){
fans=99999999;
string temp;
temp=str[1];
temp+=str[1];
len=str[1].size();
for(int i=0;i<len;i++){
str[0]=temp.substr(i,len);
tans=0;
for(int j=1;j<=n;j++){
ans=judge(j);
if(ans==-1)
return -1;
else
tans+=ans;
}
if(fans>tans) fans=tans;
}
return fans;
}
int main(){
ios::sync_with_stdio(false);
while(cin>>n){
for(int i=1;i<=n;i++)
cin>>str[i];
cout<<solve()<<endl;
}
}