HDOJ1358 Period (KMP next数组应用)

题意:n个字符的字符串,从第二个字符开始遍历。如果从第一个字符到当前字符是有循环的,那么输出当前的位置和最大循环次数。两组数据之间输出一个空格。

所以我们需要先解决如何算出(1...x)串的最大循环次数,再遍历就ok

所以如何找到最小循环节,如果(1...x)有循环x-next[x]就是循环节,可以在纸上画画,从前往后可以用“1...x-next[x]”这串字符推出整个串。而且这是最小循环节,因为next记录的是前后缀的最大匹配长度,可以反证就不说了。

如果(1...x)没有循环节,x-next[x]算出的是什么呢?1...x-next[x]“的串也可以从前往后推,就可以覆盖整个串但不是恰好覆盖,这也是解POJ
2185 Milking Grid的前提知识。

代码:

//Memory: 5060 KBTime: 188 MS
#include<cstdio>
#include<iostream>
#include<cstring>
#define maxn 1000100
using namespace std;
int n;
char str[maxn];
int next[maxn];
void getnext()
{
    next[0]=-1;
    int k=-1;
    int j=0;
    while(j<n) {
        if (k==-1||str[j]==str[k])   {
            k++;
            j++;
            next[j]=k;
            if(j%(j-k)==0&&j>1&&k!=0) printf("%d %d\n",j,j/(j-k));
        }
        else  k = next[k];
    }
}
int main()
{
    int cnt=0;
    while(scanf("%d",&n),n){
        scanf("%s",str);
        printf("Test case #%d\n",++cnt);
        getnext();
        puts("");
    }
    return 0;
}
时间: 2024-10-25 14:55:54

HDOJ1358 Period (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

LA3026 - Period(KMP)

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 the

字符串匹配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

HDOJ1358 Period 【KMP next数组应用】

题目描述 n个字符的字符串,从第二个字符开始遍历.如果从第一个字符到当前字符是有循环的,那么输出当前的位置和循环次数.两组数据之间输出一个空格. 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 解题思路 假设当前是第i个字符,那么next[i+1]就是前i个字符的串里前后最大公共串,用i-next[i+1]+1就是除了前面的公共串剩下的一部分

(KMP 1.5)hdu 1358 Period(使用next数组来求最小循环节——求到第i个字符的循环节数)

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

hdu oj Period (kmp的应用)

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

poj1961 Period(KMP)

C - Period Crawling in process... Crawling failed Time Limit:3000MS     Memory Limit:30000KB     64bit IO Format:%I64d & %I64u Submit Status Practice POJ 1961 Appoint description: System Crawler (2016-05-10) Description For each prefix of a given str

ZOJ1905Power Strings (KMP||后缀数组+RMQ求循环节)

Given two strings a and b we define a*b to be their concatenation. For example, if a = "abc" and b = "def" then a*b = "abcdef". If we think of concatenation as multiplication, exponentiation by a non-negative integer is defin

poj1961 Period kmp解决找字符串的最小循环节

/** 题目:poj1961 Period 链接:http://poj.org/problem?id=1961 题意:求从1到i这个前缀(2<=i<=N) ,如果有循环节(不能自身单独一个),输出前缀字符串长度以及最大的循环周期: 思路: 参考自:http://www.cnblogs.com/chenxiwenruo/p/3546457.html 定理:假设S的长度为len,则S存在最小循环节,循环节的长度L为len-next[len],子串为S[0-len-next[len]-1]. (1)