D - Mike and strings

D - Mike and strings

Mike has n strings s1,?s2,?...,?sn each consisting of lowercase English letters. In one move he can choose a string si, erase the first character and append it to the end of the string. For example, if he has the string "coolmike", in one move he can transform it into the string "oolmikec".

Now Mike asks himself: what is minimal number of moves that he needs to do in order to make all the strings equal?

Input

The first line contains integer n (1?≤?n?≤?50) — the number of strings.

This is followed by n lines which contain a string each. The i-th line corresponding to string si. Lengths of strings are equal. Lengths of each string is positive and don‘t exceed 50.

Output

Print the minimal number of moves Mike needs in order to make all the strings equal or print ?-?1 if there is no solution.

Example

Input

4xzzwozwoxzzzwoxxzzwo

Output

5

Input

2molzvlzvmo

Output

2

Input

3kckckc

Output

0

Input

3aaaaab

Output

-1题意:输入一个整数n,然后输入n个长度相同的字符串,如果能通过循环位移使这n个字符串都相等,输出最小的操作次数,如果不能,输出-1;题解:1.首先判断能不能通过循环位移使这n个字符串都相等    2.计算把第第1,2。。。n行字符串当模板使所有字符串都相等的操作次数    3.输出最小的值。代码
#include<iostream>
#include<string>
#include<algorithm>
#include<cstring>
#include<cstdlib>
using namespace std;
int main()
{
    string str[55];
    int i,j,n,ans[55],k,book=0;
    int x[55],b;
    cin>>n;string T,W;

    memset(x,0,sizeof(x));

    memset(ans,0,sizeof(ans));

    for(i=1;i<=n;i++)
    {
        cin>>str[i];
    }

    for(i=0;i<str[1].size();i++)
    {
        T="";
        for(j=i;j<str[1].size();j++)
            T+=str[1][j];
        for(j=0;j<i;j++)
            T+=str[1][j];
//判断是否可以通过转换换成相同的字符串
        for(j=2;j<=n;j++)
        {
            if(ans[j]==0)
            {
                if(T==str[j])
                {
                    ans[j]=1;
                }
            }
            else
                continue;
        }
    }

    for(i=2;i<=n;i++)
    {
        if(ans[i]==0)
        {
            book=1;
            break;
        }
    }

    int z;

    if(book==1)
        cout<<-1<<endl;

    else
        {
            for(i=1;i<=n;i++)
            {
                T=str[i];

                for(k=1;k<=n;k++)
                {
                    for(j=0;j<str[k].size();j++)
                    {
                        W="";
                    for(z=j;z<str[k].size();z++)
                        W+=str[k][z];
                    for(z=0;z<j;z++)
                        W+=str[k][z];
                    if(W==T)
                        {
                           b=j;
                           break;
                        }
                    }
                x[i]+=b;
                }
            }
            cout<<*min_element(x+1,x+1+n)<<endl;
        }
}
时间: 2024-10-09 22:56:02

D - Mike and strings的相关文章

Mike and strings

Mike has n strings s1, s2, ..., sn each consisting of lowercase English letters. In one move he can choose a string si, erase the first character and append it to the end of the string. For example, if he has the string "coolmike", in one move h

[卿学姐带飞系列]-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

4.30-5.1cf补题

//yy:拒绝转载!!! 悄悄告诉你,做题累了,去打两把斗地主就能恢复了喔~~~ //yy:可是我不会斗地主吖("'▽'") ~~~那就听两遍小苹果嘛~~~ 五一假期除了花时间建模,就抽空把最近没做的CF题补了点..毕竟明天开始又要继续上好多课呐...Yes, I can!(? •_•)?……(I can Huá shuǐ~~) codeforces 803 A. Maximal Binary Matrix   [简单构造] 题意:n行和n列填充零矩阵. 您要将k个1放在其中,使得得到

CodeChef 3-Palindromes(Manacher)

3-Palindromes Problem code: PALIN3 Submit All Submissions All submissions for this problem are available. Read problems statements in Mandarin Chinese and Russian as well. Mike likes strings. He is also interested in algorithms. A few days ago he dis

A - Mike and palindrome

A - Mike and palindrome Mike has a string s consisting of only lowercase English letters. He wants to change exactly one character from the string so that the resulting one is a palindrome. A palindrome is a string that reads the same backward as for

A - Mike and Fax

Time Limit:1000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Description While Mike was walking in the subway, all the stuff in his back-bag dropped on the ground. There were several fax messages among them. He concatenated these str

Codeforces548A:Mike and Fax

While Mike was walking in the subway, all the stuff in his back-bag dropped on the ground. There were several fax messages among them. He concatenated these strings in some order and now he has string s. He is not sure if this is his own back-bag or

CF 548A Mike and Fax

Descripe While Mike was walking in the subway, all the stuff in his back-bag dropped on the ground. There were several fax messages among them. He concatenated these strings in some order and now he has string s. He is not sure if this is his own bac

CodeForces 547E:Mike and Friends(AC自动机+DFS序+主席树)

What-The-Fatherland is a strange country! All phone numbers there are strings consisting of lowercase English letters. What is double strange that a phone number can be associated with several bears! In that country there is a rock band called CF con