codeforces/contest/798/problem/B

B. Mike and strings
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard output
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.

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 is to perform operations in such a way as to transform all strings into “zwoxz”.

题意:给你n个长度相同的串,对于每个串有一种操作,就是把串的第一个字符放到最后的位置,问你用这种操作把n个串变成一样,最少需要多少次操作,如果不能变成一样,则输出-1.

解题思路:首先,我们把这n个字符串都扩展一倍(就是在每个字符串后面再加一个该字符串),然后如果最后能够变成相同,那么相同的情况一定是字符串的长度种情况(未扩展前),那么我们可以枚举每一种情况,对这n个扩展之后的字符串做kmp,找出最近的匹配位置,然后把所有的加起来,最后取最小的那种情况就行。

#include<bits/stdc++.h>
using namespace std;
const int maxn = 200;
int n;
char ch[100][200];
int Next[200];
char ans[200];
void makeNext(char *s)
{
    Next[0] = 0;
    int len = strlen(s);
    int k;
    for(int i = 1,k = 0; i < len; i++)
    {
        while(k > 0&&s[i] != s[k])
        {
            k = Next[k - 1];
        }
        if(s[k] == s[i]) k++;
        Next[i] = k;
    }
}
int kmp(char *p,char *f)
{
    int len = strlen(f);
    int size = strlen(p);
    makeNext(p);
    for(int i = 0,k = 0; i < len; i++)
    {
        while(k > 0&&f[i] != p[k])
        {
            k = Next[k - 1];
        }
        if(f[i] == p[k]) k++;
        if(k == size)
        {

            return (i + 1 - size);
        }
    }
    return -1;
}
int main()
{
    scanf("%d",&n);
    for(int i = 1; i <= n; i++)
    {
        scanf("%s",ch[i]);
    }

    int lenx = strlen(ch[1]);
    for(int i = 1; i <= n; i++)
    {
        strcpy(ans,ch[i]);
        int len = lenx;
        for(int j = len,k = 0; k < len; k++,j++)
        {
            ch[i][j] = ans[k];
        }
        ch[i][2*len] = '\0';
    }
    for(int i = 1; i <= n; i++)
    {
        printf("%s\n",ch[i]);
    }
    int Min = 1e9;
    for(int i = 0,term = 1; i < 2*lenx,term <= lenx; i++,term++)
    {
        for(int j = 0,loc = i; j < lenx; j++,loc++)
        {
            ans[j] = ch[1][loc];
        }
        ans[lenx] = '\0';
        int sum = 0;
        bool flag = true;
        for(int j = 1; j <= n; j++)
        {
            int res = kmp(ans,ch[j]);
            if(res == -1)
            {
                flag = false;
            }
            sum += res;

        }
        if(flag) Min = min(Min,sum);
    }
    if(Min == 1e9) printf("-1\n");
    else printf("%d\n",Min);
    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值