F - The Minimum Length

Posted joeylee97

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了F - The Minimum Length相关的知识,希望对你有一定的参考价值。

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的主要内容,如果未能解决你的问题,请参考以下文章

Find the Minimum length Unsorted Subarray, sorting which makes the complete array sorted

hust 1010 The Minimum Length(循环节)KMP

[LeetCode] 1963. Minimum Number of Swaps to Make the String Balanced

Given a binary tree, find its minimum depth.The minimum depth is the number of nodes along the short

The service already exists! The current server installed: D:mysqlmysql-5.7.30-winx64inmysqld My(代码片

ASP.NET MVC 复制MVC项目代码到同一个项目的时候报错The request for ‘home’ has found the following matching controll(代码片