Codeforces Round #410 (Div. 2), problem: (B) Mike and strings Solution In C/C++

#include <bits/stdc++.h>
using namespace std;
string s[51];

int main()
{
int n, t, ans;
ans = INT_MAX;
cin>>n;
string temp;
for(int i=0; i<n; i++)
cin>>s[i];
for(int i=0; i<n; i++)
{
t = 0;
for(int j=0; j<n; j++)
{
temp=s[j]+s[j];
if(temp.find(s[i])==string::npos)
{
cout<<-1<<endl;
return 0;
}
t += temp.find(s[i]);
}
ans = t<ans?t:ans;
}
cout<<ans;
return 0;
}

Leave a Reply

Your email address will not be published. Required fields are marked *