B. Mike and Strings

Source: Internet
Author: User

B. Mike and strings time limit per test 2 seconds memory limit per test megabytes input standard input output standard Output

Mike has n strings s1, s2, ..., sn each consisting of lowercase 中文版 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 had the string "Coolmike", in one move he can transform it into the string "Oolmikec".

Now Mike asks Himself:what are minimal number of moves that he needs to does 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 are no solution. Examples input

4
xzzwo
zwoxz
zzwox
xzzwo
Output
5
Input
2
molzv
Lzvmo
Output
2
Input
3 KC KC KC

Output
0
Input
3
AA
AA
AB
Output
-1
Note

In the first sample TestCase the optimal scenario are to perform operations in such a-to-transform all-strings into "Zwoxz".

At first, did not consider the situation of the circulation section, and then the direct violence on the line Qaq,:-(is a wave off points, fast on 1200 qaq.

#include <bits/stdc++.h>

using namespace std;
const int maxn=3e5+7;
const int INF=1E9;
Char s[60][60];
Char s1[60];
int l;
int n;
int num[60];
int check (char *s)
{for
    (int i=0; i<l; ++i)
    {
        int J;
        for (j=0; j<l; ++j)
        {
            if (s1[j]!=s[(j+i)%l]) break;
        }
        if (j>=l) return i;
    }
    return-1;
}
int main ()
{
    scanf ("%d", &n);
    for (int i=0; i<n; ++i) scanf ("%s", S[i]);
    L=strlen (S[0]);
    int ans=inf;
    for (int i=0; i<l; ++i)
    {
        int sum=0;
        for (int j=0; j<l; ++j) s1[j]=s[0][(j+i)%l];
        for (int j=0; j<n; ++j)
        {
            int t=check (s[j]);
            if (t==-1)
            {
                puts ("-1");
                return 0;
            }
            sum+=t;
        }
        Ans=min (ans,sum);
    }
    printf ("%d\n", ans);
    return 0;
}





Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.