I use the way of brute force.Everytime I set the string t as the string that I want the remaining string to be equal to.And I caculate the steps.Then I choose the minimum steps as the result.
#include<iostream>
#include<vector>
#include<string>
#include<set>
#include<map>
#include<algorithm>
#include<queue>
using namespace std;
int count_step(string a, string b){
string compare = b + b;
int i = 0;
int len = a.size();
while (i < len){
if (a == compare.substr(i, len)) break;
else i++;
}
if (i >= len) return -1;
return i;
}
int main(){
int n,t;
while (cin >> n){
vector<string> vs(n);
for (int i = 0; i < n; i++) cin >> vs[i];
int temp;
int amount = INT_MAX;
for (int i = 0; i < n; i++){
temp = 0;
for (int j = 0; j < n; j++){
if (i != j){
t = count_step(vs[i], vs[j]);
if (t == -1){
cout << "-1" << endl;
break;
}
temp += t;
}
}
if (t == -1) break;
amount = min(amount, temp);
}
if (t == -1) continue;
cout << amount << endl;
}
return 0;
}