poj1961Period【kmp next数组】

大意:跟poj2406一样的题  思路见http://www.cnblogs.com/zhanzhao/p/4761477.html

代码:

 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 using namespace std;
 5
 6 const int maxn = 1000005;
 7
 8 int next[maxn];
 9
10 void get(char *s) {
11     int l = strlen(s);
12     int j = 0, k = -1;
13     next[0] = -1;
14     while(j < l) {
15         if(k == -1 || s[j] == s[k]) {
16
17
18
19 //            if(s[++j] == s[++k]) {
20 //                next[j] = next[k];
21 //            } else {
22 //                next[j] = k;
23 //            }
24             next[++j] = ++k;
25         } else {
26             k = next[k];
27         }
28     }
29 }
30 char s[maxn];
31
32 int main() {
33     int n;
34     int kase = 1;
35     while(scanf("%d",&n) && n) {
36         scanf("%s",s);
37         printf("Test case #%d\n", kase++);
38         get(s);
39         int l = strlen(s);
40 //        for(int i = 0; i <= l; i++) {
41 //            printf("%d ", next[i]);
42 //        }puts("");
43         for(int i = 2; i <= l; i++) {
44             int ans = 1;
45             if(i % (i - next[i]) == 0) {
46                 ans = i / (i - next[i]);
47             }
48             if(ans > 1) {
49                 printf("%d %d\n", i, ans);
50             }
51         }
52         puts("");
53     }
54 }

时间: 2024-08-24 04:58:18

poj1961Period【kmp next数组】的相关文章

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

字符串匹配KMP next数组的理解

#include<cstdio> #include<cstring> void getNext(int *Next,char* src){ int i,j; Next[0]=-1; i=0; j=-1; int N=strlen(src); while(i<N-1){ if(j==-1||src[i]==src[j]){ ++i; ++j; Next[i]=j; }else{ /* 理解难点:假设已经存在Next:假设是两个字符串在进行比较. 1. a)现在有两个字符串 sr

Poj1961--Period(Kmp, Next数组求循环节长度 &amp;&amp; 出现次数)

Period Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 14657   Accepted: 6967 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

poj1961--Period(KMP求一个串的重复子串)

Period Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 13949   Accepted: 6601 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

POJ-3450 Corporate Identity (KMP+后缀数组)

Description Beside other services, ACM helps companies to clearly state their "corporate identity", which includes company logo but also other signs, like trademarks. One of such companies is Internet Building Masters (IBM), which has recently a

poj2406(kmp next数组)

大意:给出一个字符串 问它最多由多少相同的字串组成 如  abababab由4个ab组成 分析: kmp中的next数组求最小循环节的应用 例如 ababab  next[6] = 4; 即 ababab ababab 1~4位  与2~6位是相同的 那么前两位 就等于3.4位 3.4位就等于5.6位 …… 所以 如果 能整除  也就循环到最后了 如果不能整除 就最后余下的几位不在循环内 例如 1212121 1212121 最后剩余1不能等于循环节 代码: 1 #include <iostre

[hdu3336]kmp(后缀数组)

题意:求字符串s的所有前缀出现次数之和. http://www.cnblogs.com/jklongint/p/4446117.html 思路:用kmp做,简单且效率高.以前缀结尾的位置分类,令dp[i]为以结尾位置在i的前缀数量,那么dp[i] = cnt(j)(j~i是前缀),而由kmp的next函数的转移性质,可得如下递推方程:dp[i] = dp[next[i]] + 1,把这个递推式不断展开,也就是i = next[i]不断迭代,那么+1的个数就是dp[i] = cnt(j)(j~i是

POJ2406 Power Strings(KMP,后缀数组)

这题可以用后缀数组,KMP方法做 后缀数组做法开始想不出来,看的题解,方法是枚举串长len的约数k,看lcp(suffix(0), suffix(k))的长度是否为n- k ,若为真则len / k即为结果. 若lcp(suffix(0), suffix(k))的长度为n- k,则将串每k位分成一段,则第1段与第2段可匹配,又可推得第2段与第3段可匹配……一直递归下去,可知每k位都是相同的,画图可看出匹配过程类似于蛇形. 用倍增算法超时,用dc3算法2.5秒勉强过. #include<cstdi

POJ 2406 KMP/后缀数组

题目链接:http://poj.org/problem?id=2406 题意:给定一个字符串,求由一个子串循环n次后可得到原串,输出n[即输出字符串的最大循环次数] 思路一:KMP求最小循环机,然后就能求出循环次数. #define _CRT_SECURE_NO_DEPRECATE #include<iostream> #include<cstdio> #include<cstring> #include<algorithm> #include<str