首页 > 代码库 > [卿学姐带飞系列]-Codeforces Round #410 (Div. 2)_B - Mike and strings

[卿学姐带飞系列]-Codeforces Round #410 (Div. 2)_B - Mike and strings

 1 #include<bits/stdc++.h>
 2 #define inf 0x3f3f3f3f
 3 using namespace std;
 4 const int maxn=55;
 5 string s[maxn];
 6 int main()
 7 {
 8     int n;
 9     cin>>n;
10     for(int i=0;i<n;i++){
11         cin>>s[i];
12     }
13     int ans=inf,tem;
14     for(int i=0;i<s[0].size();i++){
15         tem=i;
16         string s1=s[0].substr(i,s[0].size()-i)+s[0].substr(0,i);
17         for(int j=1;j<n;j++){
18             int flag=0;
19             for(int k=0;k<s[j].size();k++){
20                 string s2=s[j].substr(k,s[j].size()-k)+s[j].substr(0,k);
21                 if(s1==s2){
22                     tem += k;
23                     flag=1;
24                     break;
25                 }
26             }
27             if(!flag) {
28                 puts("-1\n"); return 0;
29             }
30         }
31         ans = min(ans,tem);
32     }
33     cout<<ans<<endl;
34     return 0;
35 }

 

[卿学姐带飞系列]-Codeforces Round #410 (Div. 2)_B - Mike and strings