Period[HDU1358]
Period
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2120 Accepted Submission(s): 1039
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
#include<stdio.h>
#include<string.h>
#define gs 1000010
int next[gs];
char str[gs];
void GetNext(char * str,int * next)
{
int N=strlen(str+);
next[]=;
int j=;
for(int i=;i<=N;i++)
{
while(j> && str[j+]!=str[i]) j=next[j];
if(str[j+]==str[i]) j+=;
next[i]=j;
}
}
int main()
{
int N,cas=;
while (scanf("%d",&N)!=EOF)
{
if (N==) return ;
cas++;
printf("Test case #%d\n",cas);
scanf("%s",str+);
GetNext(str,next);
for (int i=;i<=N;i++)
if (i%(i-next[i])== && i/(i-next[i])>) printf("%d %d\n",i,i/(i-next[i]));
printf("\n");
}
return ;
}
Period[HDU1358]的更多相关文章
- HDU1358 Period —— KMP 最小循环节
题目链接:https://vjudge.net/problem/HDU-1358 Period Time Limit: 2000/1000 MS (Java/Others) Memory Lim ...
- poj1961 & hdu1358 Period【KMP】
Period Time Limit: 3000MS Memory Limit: 30000K Total Submissions: 20436 Accepted: 9961 Descripti ...
- kuangbin专题十六 KMP&&扩展KMP HDU1358 Period
For each prefix of a given string S with N characters (each character has an ASCII code between 97 a ...
- HDU-1358 Period 字符串问题 KMP算法 求最小循环节
题目链接:https://cn.vjudge.net/problem/HDU-1358 题意 给一个字符串,对下标大于2的元素,问有几个最小循环节 思路 对每个元素求一下minloop,模一下就好 提 ...
- hdu1358 Period
首先给个博客:http://blog.csdn.net/lttree/article/details/20732385 感觉他说的很好,尤其是引用的那个博客,清晰的说明了循环节的两个公式. http: ...
- HDU1358:Period
第一次做KMP.还没有理解透. 在自己写一遍时没有让next[0]初始化为-1. 还有就是next应该是c++中的关键字,提交后编译错误. From: http://blog.csdn.net/lib ...
- [KMP求最小循环节][HDU1358][Period]
题意 求所有循环次数大于1的前缀 的最大循环次数和前缀位置 解法 直接用KMP求最小循环节 当满足i%(i-next[i])&&next[i]!=0 前缀循环次数大于1 最小循环节是i ...
- hdu1358 Period kmp求循环节
链接 http://acm.hdu.edu.cn/showproblem.php?pid=1358 思路 当初shenben学长暑假讲过,当初太笨了,noip前几天才理解过来.. 我也没啥好说的 代码 ...
- hdu1358 Period KMP
给出一个字符串,找出所有可以作为它循环节的子串长度 利用kmp的失配数组的性质,可以直接做 #include<stdio.h> #include<string.h> ; cha ...
随机推荐
- Android四大组件之Service
Android四大组件之Service Android支持服务的概念,服务是在后台运行的组件,没有用户界面,Android服务可用有与活动独立的生命周期.Android支持两种类型的服务: 本地服务: ...
- UITableView 委托方法总结
http://blog.sina.com.cn/s/blog_7b9d64af01019x3t.html 总结: UITableViewDelegate row: heightForRow hea ...
- Shell脚本中cd命令使用
在写shell脚本的时候发现cd切换目录的时候无法切换,代码是下面的. #!/bin/bash #changedir.sh history cd /home/firefox sleep pwd 我仔细 ...
- HDOJ 1856
#include<cstdio> #include<cstdlib> typedef struct ufse *ufset; struct ufse { ]; ]; }UFS; ...
- Linux下常用程序的代理服务器(proxy)配置
Linux下有很多程序都只有命令行接口,对于这类程序,它们通过代理服务器(proxy)访问网络的方式也不尽相同.在本文中Easwy总结了一些常用Linux程序配置代理服务器的方法. [ 通用代理服务器 ...
- Xenomai
http://blog.csdn.net/robertsong2004/article/details/43889249 嵌入式系统的开发,如果对实时性要求不高,就可以使用Linux自身的实时补丁实现 ...
- DP:Multiplication Puzzle(POJ 1651)
卡片游戏 题目大意:给你一排卡片,你可以从从中抽一些卡片(但是不能抽最左和最右的卡片),每张卡片上有一个数字,当你从中抽出一张卡片后,你将得卡片的数字和卡片左右两张卡片的数字的乘积的分数,问当剩下最左 ...
- codeforces A. IQ Test 解题报告
题目链接:http://codeforces.com/problemset/problem/328/A 一开始单纯地直接判断给出的序列是等差还是等比,连这一句“You should also prin ...
- VS添加lib库
#pragma comment(lib,"opengl32.lib")
- Android activity_main.xml删除边缘距离,充满屏幕
删除android:paddingBottom.android:paddingLeft.android:paddingRight和android:paddingTop四个属性