KMP + 求最小循环节 --- HDU 1358 Period

Period

Problem‘s Link: http://acm.hdu.edu.cn/showproblem.php?pid=1358



Mean:

给你一个字符串,让你从第二个字符开始判断当前长度的字符串是否是重复串,如果是,输出当前位置,并输出重复串的周期。

analyse:

还是next数组的运用,详见上一篇博客。

Time complexity: O(N)

Source code: 

/*
* this code is made by crazyacking
* Verdict: Accepted
* Submission Date: 2015-07-28-07.12
* Time: 0MS
* Memory: 137KB
*/
#include <queue>
#include <cstdio>
#include <set>
#include <string>
#include <stack>
#include <cmath>
#include <climits>
#include <map>
#include <cstdlib>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#define  LL long long
#define  ULL unsigned long long
using namespace std;
const int MAXN=1000010;
int n;
char s[MAXN];
int Next[MAXN];
void getNext()
{
     Next[0]=0;
     for(int i=1,k=0;i<n;++i)
     {
           while(s[i]!=s[k]&&k) k=Next[k-1];
           if(s[i]==s[k]) ++k;
           Next[i]=k;
     }
}
int main()
{
     ios_base::sync_with_stdio(false);
     cin.tie(0);
     int cas=1;
     while(~scanf("%d",&n)&&n)
     {
           scanf("%s",s);
           getNext();
           printf("Test case #%d\n",cas++);
           for(int i=1;i<n;++i)
           {
                 int now_cycle=(i+1)-Next[i];
                 if((now_cycle!=i+1) && (i+1)%now_cycle==0)
                 {
                       printf("%d %d\n",i+1,(i+1)/now_cycle);
                 }
           }
           puts("");
     }
     return 0;
}
/*

*/

时间: 2024-10-18 16:35:52

KMP + 求最小循环节 --- HDU 1358 Period的相关文章

模板题 + KMP + 求最小循环节 --- HDU 3746 Cyclic Nacklace

Cyclic Nacklace Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=3746 Mean: 给你一个字符串,让你在后面加尽量少的字符,使得这个字符串成为一个重复串. 例: abca---添加bc,成为abcabc abcd---添加abcd,成为abcdabcd aa---无需添加 analyse: 经典的求最小循环节. 首先给出结论:一个字符串的最小循环节为:len-next[len]. 证明: 举个例子:abcab

KMP + 求最小循环节 --- POJ 2406 Power Strings

Power Strings Problem's Link: http://poj.org/problem?id=2406 Mean: 给你一个字符串,让你求这个字符串最多能够被表示成最小循环节重复多少次得到. analyse: KMP之next数组的运用.裸的求最小循环节. Time complexity: O(N) Source code:  /** this code is made by crazyacking* Verdict: Accepted* Submission Date: 20

(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

nyoj 329 循环小数【KMP】【求最小循环节长度+循环次数+循环体】

循环小数 时间限制:3000 ms  |  内存限制:65535 KB 难度:1 描述 我们可爱的 c小加 近段儿正在潜心研究数学,当他学习到循环小数这一部分时不是太明白循环体是什么意思(比如说3.23232323的循环体是23.2323.23232323),假设我们现在的循环小数都是严格循环的并且有限的,也就是说不出现2.16666666(循环体为6,长度为1)的情况,只有123123这样的循环出现.给他一个小数,他需要找出最小循环体的长度.循环体和循环的次数,请你帮他解决这个问题. 输入 输

poj2406 kmp 求最小循环字串

Power Strings Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 47748   Accepted: 19902 Description 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 = "

poj 1961 Period【求前缀的长度,以及其中最小循环节的循环次数】

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

hdu 1358 Period 最小循环节

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1358 分析:已知字符串,求其由最小循环节构成的前缀字符串. /*Period Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 3507 Accepted Submission(s): 1766 Problem Description For ea

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 3746 Cyclic Nacklace (KMP求最小循环节)

//len-next[len]为最小循环节的长度 # include <stdio.h> # include <algorithm> # include <string.h> using namespace std; int len; char a[100010]; int next[100010]; void Getnext() { int i=0,j=-1; next[0]=-1; while(i<=len) { if(j==-1||a[i]==a[j]) i