题目链接:http://codeforces.com/problemset/problem/798/B点击打开链接
至于判断负一的情况 可以根据次数是否超过了对一个字符串的所有循环次数即可
#include <bits/stdc++.h>
using namespace std;
int main()
{
string s[55];
int n;
cin >> n;
for(int i=0;i<n;i++)
{
cin >> s[i];
}
int ans=n*s[0].length();
int flag=0;
for(int i=0;i<=s[0].length();i++)
{
int cnt=i;
if(i!=0)
{
char c=s[0][0];
s[0].erase(s[0].begin());
s[0]+=c;
}
for(int j=1;j<n;j++)
{
string mid=s[j];
int ccnt=0;
while(mid!=s[0]&&ccnt<=mid.length())
{
ccnt++;
char c=mid[0];
mid.erase(mid.begin());
mid+=c;
}
if(ccnt>mid.length())
flag=1;
cnt+=ccnt;
}
ans=min(ans,cnt);
}
if(flag)
cout << "-1" << endl;
else
cout << ans << endl;
}