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. java语言的各种输入情况(ACM常用)

    1.只输入一组数据: Scanner s=new Scanner(System.in); int a=s.nextInt(); int b=s.nextInt(); 2.输入有多组数据,没有说明输入几 ...

  2. 谈缓存和Redis

    自从上次分享<Redis到底该如何利用?>已经有1年多了,这1年经历了不少.从码了我们网站的第一行开始到现在,我们的缓存模块也不断在升级,这之中确实略有心得,最近也有朋友探讨缓存,觉得可以 ...

  3. C++学习笔记: 智能指针

    c++ 智能指针学习新的 class Simple { public: Simple() { number = param; std::cout << "Simple: &quo ...

  4. ThreeJS geometry的顶点世界坐标

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

  5. css3+javascript实现翻页幻灯片

    先上效果图 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <tit ...

  6. Android深入四大组件(六)Service的启动过程

    前言 此前我用较长的篇幅来介绍Android应用程序的启动过程(根Activity的启动过程),这一篇我们接着来分析Service的启动过程.建议阅读此篇文章前,请先阅读Android深入四大组件(一 ...

  7. git中常见操作指令

    从git上拉下一个项目: 1.git clone 项目链接                  2. git checkout development (切换到development分支)       ...

  8. ls 操作命令 -l/-R和rm -r dir 功能实现

    ls -R #include <sys/stat.h> #include <dirent.h> #include <fcntl.h> #include <st ...

  9. 让UpdatePanel支持文件上传(1):开始 .

    UpdatePanel从一开始就无法支持AJAX的文件上传方式.Eilon Lipton写了一篇文章解释了这个问题的原因.文章中提供了两个绕开此问题的方法: 将“上传”按钮设为一个传统的PostBac ...

  10. [翻译] PQFCustomLoaders

    PQFCustomLoaders Current version: 0.0.1 Collection of highly customizable loaders for your iOS proje ...