bzoj1355——2016——3——15

传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=1355

题目大意:

1355: [Baltic2009]Radio Transmission

Time Limit: 10 Sec  Memory Limit: 64 MB
Submit: 591  Solved: 390
[Submit][Status][Discuss]

Description

给你一个字符串,它是由某个字符串不断自我连接形成的。 但是这个字符串是不确定的,现在只想知道它的最短长度是多少.

Input

第一行给出字符串的长度,1 < L ≤ 1,000,000. 第二行给出一个字符串,全由小写字母组成.

Output

输出最短的长度

Sample Input

8
cabcabca

Sample Output

3

HINT

对于样例,我们可以利用"abc"不断自我连接得到"abcabcabc",读入的cabcabca,是它的子串

Source

题解:这题就是KMP next数组的应用啦(水),最小值就是n-next[n](很容易想吧,因为可行解集为{n-next[n],n-next[next[n]]....)所以最小解显然为n-next[n];

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #define inf 0x7fffffff
 5 int n,ans;
 6 int next[1000100];
 7 char s[1000100];
 8 using namespace std;
 9 int main()
10 {
11     scanf("%d",&n);
12     scanf("%s",s+1);
13     ans=0;
14     int fix=0;
15     for (int i=2; i<=n; i++)
16     {
17         while (fix && s[fix+1]!=s[i]) fix=next[fix];
18         if (s[fix+1]==s[i]) fix++;
19         next[i]=fix;
20     }
21     printf("%d
",n-next[n]);
22 }
View Code
我太蒟蒻了,所以神犇们留下意见让我跪膜
原文地址:https://www.cnblogs.com/HQHQ/p/5280425.html