hdu 1358(KMP--next[]数组应用

Period

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 13060    Accepted Submission(s): 6102

Problem Description

For each prefix of a given string S with N characters (each character has an ASCII code between 97 and 126, inclusive), we want to know whether the prefix is a periodic string. That is, for each i (2 <= i <= N) we want to know the largest K > 1 (if there is one) such that the prefix of S with length i can be written as AK , that is A concatenated K times, for some string A. Of course, we also want to know the period K.

Input

The input file consists of several test cases. Each test case consists of two lines. The first one contains N (2 <= N <= 1 000 000) – the size of the string S. The second line contains the string S. The input file ends with a line, having the number zero on it.

Output

For each test case, output “Test case #” and the consecutive test case number on a single line; then, for each prefix with length i that has a period K > 1, output the prefix size i and the period K separated by a single space; the prefix sizes must be in increasing order. Print a blank line after each test case.

Sample Input

3

aaa

12

aabaabaabaab

0

Sample Output

Test case #1

2 2

3 3

Test case #2

2 2

6 2

9 3

12 4

 1 /**
 2     Finished time:9.15.2018
 3     Author : hyacinth
 4     Algorithm : KMP--next[]
 5 */
 6 #include<iostream>
 7 #include<cstdio>
 8 #include<cstring>
 9 using namespace std;
10 const int maxn=1000010;
11
12 int nex[maxn]={-1,0};
13 char buf[maxn];
14
15 void Get_Next(const char s[])
16 {
17     int len=strlen(s);
18     int index=2;
19     int cn=0;
20     while(index <= len)
21     {
22         if(s[index-1] == s[cn])
23             nex[index++]=++cn;
24         else if(nex[cn] != -1)
25             cn=nex[cn];
26         else
27             nex[index++]=0;
28     }
29 }
30 int main()
31 {
32     int N;
33     int kase=1;
34     while(~scanf("%d",&N) && N)
35     {
36         scanf("%s",buf);
37         Get_Next(buf);
38         int len=strlen(buf);
39         printf("Test case #%d\n",kase++);
40         for(int i=2;i <= len;++i)
41         {
42             int ans=i-nex[i];
43             int k=i/ans;
44             if(i%ans == 0 && k > 1)
45                 printf("%d %d\n",i,k);
46         }
47         printf("\n");
48     }
49 }

原文地址:https://www.cnblogs.com/violet-acmer/p/9650785.html

时间: 2025-01-17 19:54:57

hdu 1358(KMP--next[]数组应用的相关文章

hdu 1358 KMP next数组的运用

题意:给一个字符串,从第二个字符开始,判断前面的是不是循环串,是的话就输出当前位置和循环次数. 考的是对于next数组的理解和灵活运用,字符编号从0开始,那么if(i%(i-next[i])==0),则i前面的串为一个循环串,其中循环子串出现i/(i-next[i])次. 1 #include<iostream> 2 #include<string> 3 #include<string.h> 4 #define MAX_N 1000005 5 6 using names

HDU 1358 (KMP)

Period Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 5859    Accepted Submission(s): 2844 Problem Description For each prefix of a given string S with N characters (each character has an ASCI

hdu 3336 kmp+next数组应用

分析转自:http://972169909-qq-com.iteye.com/blog/1114968 十分易懂 题意:求字串中[前缀+跟前缀相同的子串]的个数? Sample Input 1 4 abab Sample Output 6 abab:包括2个a,2个ab,1个aba,1个abab 这里要用到next值的意义: next[i]表示前i个字符所组成的字符串的最大前后缀匹配长度 举个例子: next[5]=2, 表示下标5前面那个字符串abcab的前后缀匹配的最大长度是2,显然就是ab

poj1961 &amp; hdu 1358 Period(KMP)

poj 题目链接:http://poj.org/problem?id=1961 hdu题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1358 Description For each prefix of a given string S with N characters (each character has an ASCII code between 97 and 126, inclusive), we want to know whether

HDU 1358 next数组的推移

题目大意: 输入n,再输入一个长度为n的字符串,从第二位开始,计算它的前缀(包括他自己)中出现过的重复字符串的个数,如aabaabaabaab的第6位的前缀aabaab,aab连续出现了两次,所以输出位数i=6,k=2 这个题目要利用next函数求解,不断往前推移,保证往前推移的量能被i整除. 即del=i-next[i]: 保证i%del==0: 其实i%del==0成立之后不用多想了,已经可以保证前面是轮回字符串了 在此稍微进行一下理解,如next[9]=6:那么1,2,3位上的元素可以往前

hdu 3333 树状数组+离线处理

http://acm.hdu.edu.cn/showproblem.php?pid=3333 不错的题,想了很久不知道怎么处理,而且答案没看懂,然后找个例子模拟下别人的代码马上懂了---以后看不懂的话就拿个例子模拟下别人的代码 举个例子:1 3 3 5 3 5 查询 a, 2 4 b, 2 5 最初是这么想的:对于a查询,倘若把第二个数第三个数变成1个3,那么到b查询,又出现了两个3,再做处理似乎还是O(n),而且如果先出现2,5查询,后出现2,4查询,那么还需要把删除的数补回来.....o(╯

Hdu 3887树状数组+模拟栈

题目链接 Counting Offspring Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1757    Accepted Submission(s): 582 Problem Description You are given a tree, it’s root is p, and the node is numbered fr

HDU 1754 树状数组 解法

mnesia在频繁操作数据的过程可能会报错:** WARNING ** Mnesia is overloaded: {dump_log, write_threshold},可以看出,mnesia应该是过载了.这个警告在mnesia dump操作会发生这个问题,表类型为disc_only_copies .disc_copies都可能会发生. 如何重现这个问题,例子的场景是多个进程同时在不断地mnesia:dirty_write/2 mnesia过载分析 1.抛出警告是在mnesia 增加dump

HDOJ3336 Count the string 【KMP前缀数组】+【动态规划】

Count the string Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 4212    Accepted Submission(s): 1962 Problem Description It is well known that AekdyCoin is good at string problems as well as n

hdu 1711 KMP模板题

// hdu 1711 KMP模板题 // 贴个KMP模板吧~~~ #include <cstdio> #include <cstring> #include <algorithm> #include <iostream> using namespace std; const int MAX_N = 1000008; const int MAX_M = 10008; int T[MAX_N]; int p[MAX_M]; int f[MAX_M]; int