Period

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

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

 
Recommend
JGShining   |   We have carefully selected several similar problems for you:  1686 3336 3746 3068 2203 
 
题目大意:
给一个字符串,从第二个字符开始,让你判断前面字符串是否具有周期性,然后输出此位置和最大周期数。(周期要大于一)
 
思路:
先构造出 next[] 数组,下标为 i,定义一个变量 j = i - next[i] 就是next数组下标和下标对应值的差,如果这个差能整除下标 i,即 i%j==0 ,则说明下标i之前的字符串(周期性字符串长度为 i)一定可以由一个前缀周期性的表示出来,这个前缀的长度为刚才求得的那个差,即 j,则这个前缀出现的次数为 i/j 。所以最后输出i和i/j即可。
 
code:
 
#include<cstdio>
#include<iostream>
#include<cstring>
#include<memory>
using namespace std;
char wenben[];
int next1[];
void getnext1(char* s,int* next1,int m)
{
next1[]=;
next1[]=;
for(int i=;i<m;i++)
{
int j=next1[i];
while(j&&s[i]!=s[j])
j=next1[j];
if(s[i]==s[j])
next1[i+]=j+;
else
next1[i+]=;
}
}
int main()
{
int L;
int t=;
while(~scanf("%d",&L))
{
if(L==)
break;
scanf("%s",wenben);
getnext1(wenben,next1,L);
for(int i=;i<L;i++)
printf("i=%d->%d ",i,next1[i]);
printf("\n");
printf("Test case #%d\n",t++);
for(int i=;i<=L;i++)
{
int k=i-(next1[i]);
if(k!=i&&(i)%k==)
printf("%d %d\n",i,i/k);
}
printf("\n");
}
return ;
}

HDU 1358 Period 求前缀长度和出现次数(KMP的next数组的使用)的更多相关文章

  1. hdu 1358 Period (KMP求循环次数)

    Problem - 1358 KMP求循环节次数.题意是,给出一个长度为n的字符串,要求求出循环节数大于1的所有前缀.可以直接用KMP的方法判断是否有完整的k个循环节,同时计算出当前前缀的循环节的个数 ...

  2. [HDU 1358]Period[kmp求周期]

    题意: 每一个power前缀的周期数(>1). 思路: kmp的next. 每一个前缀都询问一遍. #include <cstring> #include <cstdio> ...

  3. hdu 1358 period KMP入门

    Period 题意:一个长为N (2 <= N <= 1 000 000) 的字符串,问前缀串长度为k(k > 1)是否是一个周期串,即k = A...A;若是则按k从小到大的顺序输 ...

  4. 【HDU 1358 Period】

                 Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)     ...

  5. HDU 3746 - Cyclic Nacklace & HDU 1358 - Period - [KMP求最小循环节]

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3746 Time Limit: 2000/1000 MS (Java/Others) Memory Li ...

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

    Period Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=1358 Mean: 给你一个字符串,让你从第二个字符开始判断当前长度 ...

  7. Hdu 1358 Period (KMP 求最小循环节)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1358 题目描述: 给出一个字符串S,输出S的前缀能表达成Ak的所有情况,每种情况输出前缀的结束位置和 ...

  8. HDU 1358 Period (kmp求循环节)(经典)

    <题目链接> 题目大意: 意思是,从第1个字母到第2字母组成的字符串可由某一周期性的字串(“a”) 的两次组成,也就是aa有两个a组成: 第三行自然就是aabaab可有两个aab组成: 第 ...

  9. hdu 1358 Period(kmp求一个串的重复子串)

    题意:统计单串中从某个位置以前有多少重复的串 思路:kmp模板 #include<iostream> #include<stdio.h> #include<string. ...

随机推荐

  1. 牛客Wannafly挑战赛23F 计数(循环卷积+拉格朗日插值/单位根反演)

    传送门 直接的想法就是设 \(x^k\) 为边权,矩阵树定理一波后取出 \(x^{nk}\) 的系数即可 也就是求出模 \(x^k\) 意义下的循环卷积的常数项 考虑插值出最后多项式,类比 \(DFT ...

  2. 03.CSS动画-->自定义动画

    <!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  3. HTML表单相关

    表单:<input type="text" name="" value="" size="显示字符数" maxle ...

  4. 经典的 div + css 鼠标 hover 下拉菜单

    经典的 div + css 鼠标 hover 下拉菜单 效果图: 源码: <html> <head> <meta charset="utf-8"> ...

  5. 控制HTML页面内容不能选中的方法

    方法有二 一: css 方法 user-seletct: none;-webkit-user-seletct: none;-moz-user-seletct: none;-ms-user-seletc ...

  6. org.apache.ibatis.binding.BindingException: Invalid bound statement (not found): cn.gaiay.business.helper.dao.LiveRegenrationRecordMapper.insert

    原因分析: 字段名称.报名.类名 对应不上 ,比如colomn和property属性 反了.. 按以下步骤一一执行: 1:检查xml文件所在的package名称是否和interface对应的packa ...

  7. java将int类型的变量转化成String类型的

    第一种方法:String的valueOf方法,int i=5;String s=String.valueOf(i);第二种方法,直接在int后面加一个空的字符串,因为在java里面,默认任务int类型 ...

  8. [翻译] ATTutorialController

    ATTutorialController https://github.com/AfonsoTsukamoto/ATTutorialController A simple to use tutoria ...

  9. C++程序暂停

    //这里的getchar();用来暂停程序,以便查看程序输出的内容 //也可以用system("pause");等来代替

  10. Linux bash内置命令集

    man cd  -->查询不到,所以会提示bash的内置命令 . alias bg bind break builtin caller cd command compgen complete c ...