码迷,mamicode.com
首页 > 其他好文 > 详细

F - The Minimum Length

时间:2017-04-06 20:05:42      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:sam   for   abc   ini   ble   char   ios   new   osi   

There is a string A. The length of A is less than 1,000,000. I rewrite it again and again. Then I got a new string: AAAAAA...... Now I cut it from two different position and get a new string B. Then, give you the string B, can you tell me the length of the shortest possible string A. For example, A="abcdefg". I got abcdefgabcdefgabcdefgabcdefg.... Then I cut the red part: efgabcdefgabcde as string B. From B, you should find out the shortest A.InputMultiply Test Cases. For each line there is a string B which contains only lowercase and uppercase charactors. The length of B is no more than 1,000,000.OutputFor each line, output an integer, as described above.Sample Input

bcabcab
efgabcdefgabcde

Sample Output

3
7
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<vector>
#include<string>
#include<cstring>
using namespace std;
#define MAXN 1000001
typedef long long LL;
/*
给定一个串,找最短循环节
*/
char s[MAXN];
int Next[MAXN];
void kmp_pre(int m)
{
    int j,k;
    j = 0;k = Next[0] = -1;
    while(j<m)
    {
        if(k==-1||s[j]==s[k])
            Next[++j] = ++k;
        else
            k = Next[k];
    }
}
int main()
{
    while(scanf("%s",s)!=EOF)
    {
        int l = strlen(s);
        kmp_pre(l);
        int ans = l - Next[l];
        printf("%d\n",ans);
    }
}

 

F - The Minimum Length

标签:sam   for   abc   ini   ble   char   ios   new   osi   

原文地址:http://www.cnblogs.com/joeylee97/p/6674853.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!