Period

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

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
题目大意:一个字符串,问从头到某个位置,字符串的前缀最多重复了多少次。比方aaaa的字符串,到第二个字符,前缀a重复了两次,到第三个字符,前缀a重复了三次,到第四个字符,前缀a重复了四次,前缀aa重复了两次,但我们要得到的是重复了四次。
解题思路:考察KMP算法中的p数组。p数组是基本的,然后,在p数组中考察,p[6]=4,我们能知道的是,s[1]=s[3]=s[5],s[2]=s[4]=s[6],其实只要在p[k]=u时,当u整除(k-u)的时候,就是满足题目要求的时候。
 AC代码(一):
 #include <stdio.h>
#include <string.h>
#define N 1000000 int n;
char str[N];
int p[N]; void run(void)
{
memset(p,-,sizeof(p));
for(int i=;i<n;i++)
{
int k=p[i-];
while()
{
if(str[k+]==str[i])
{
p[i]=k+;
if((i+)%(i-p[i])==)
printf("%d %d\n",i+,(i+)/(i-p[i]));
break;
}
if(k==-)break;
k=p[k];
}
}
} int main()
{
int id=;
while(scanf("%d",&n)==&&n)
{
getchar();
gets(str);
printf("Test case #%d\n",++id);
run();
puts("");
}
}

AC代码(二):

#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<queue>
#include<string>
#include<cmath>
using namespace std;
const int M = 1e6+;
char s[M];
int next[M];
void solve()
{
int j=,k=-;
next[]= -;
while(s[j]!='\0')
{
if(k == -)
{
next[++j] = ;
k=;
}
if(s[k] == s[j])
{
k++;
next[++j] = k;
}
else k = next[k];
}
}
int main()
{
int n,id = ;
while(cin>>n && n)
{
scanf("%s",s);
int len = strlen(s);
printf("Test case #%d\n",++id);
solve();
for(int i=; i<=len; i++)
{
int j = i-next[i];
if(i%j == && i/j>)
printf("%d %d\n",i,i/j);
}
printf("\n");
}
return ;
}

HDU 1358 Period(kmp简单解决)的更多相关文章

  1. HDU 1358 Period KMP

    题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1358 求周期问题,简单KMP—— AC代码: #include <iostream> # ...

  2. hdu 1358 period KMP入门

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

  3. HDU 1358 Period(KMP计算周期)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1358 题目大意:给你一串字符串,判断字符串的前缀是否由某些字符串多次重复而构成. 也就是,从第1个字母 ...

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

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

  5. hdu 1358 Period(KMP入门题)

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

  6. HDU 1358 Period(KMP next数组运用)

    Period Problem Description For each prefix of a given string S with N characters (each character has ...

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

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

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

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

  9. 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 ...

随机推荐

  1. Android 卡顿优化 4 布局优化实际技巧

    今天分享一些layout布局书写中的一些技巧,希望看过之后你也一样可以写出性价比高的布局.我个人的目标是用最少的View写出一样效果的布局.因为我相信View的数量减少伴随着的就是层级的减少.从而达到 ...

  2. 基于Python的交互式访问

    应用迁移中遇到一些有特殊要求的应用,比如需要通过交互生成一些新的config文件,然后启动应用需要依赖于这些文件,这样在构建镜像的时候基本上是没有办法把这些文件固定的,因为他需要根据运行环境去进行动态 ...

  3. WebLogic 1036的镜像构建

    WebLogic1035版本构建出来的镜像有问题,部署应用激活后返回的是后面pod的ip和端口号,在1036版本和12.1.3版本都无此问题. weblogic 1036的Dockerfile [ro ...

  4. InfluxDB写流程

    Influxdb version1.8 HTTP: 0x00000000016d0ce3 in github.com/influxdata/influxdb/coordinator.(*PointsW ...

  5. Python datetime 格式化字符串:strftime()

    Python datetime 格式化字符串:strftime()   Python 的datetime模块 其实就是date和time 模块的结合, 常见的属性方法都比较常用 比如: datetim ...

  6. urllib库在python2和python3环境下的使用区别

    好东西啊!!! Python 2 name Python 3 name urllib.urlretrieve() urllib.request.urlretrieve() urllib.urlclea ...

  7. ocx注册

    (1)服务器OCX注册 (2)IE浏览器,站点加入可信任站点. internet 选项->安全->可信任站点.把“对该区域中的所有站点要求服务器验证(https:)” 前面的勾去掉 (3) ...

  8. 解决RegexKitLite编译报错

    原地址:http://blog.csdn.net/kepoon/article/details/7586861 在编译RegexKitLite的时候,报错如下: Undefined symbols f ...

  9. 算法笔记_161:算法提高 十进制数转八进制数(Java)

    目录 1 问题描述 2 解决方案   1 问题描述 编写函数,其功能为把一个十进制数转换为其对应的八进制数.程序读入一个十进制数,调用该函数实现数制转换后,输出对应的八进制数. 样例输入 9274 样 ...

  10. FZU1920 Left Mouse Button(dfs)

     Problem 1920 Left Mouse Button Accept: 385    Submit: 719 Time Limit: 1000 mSec    Memory Limit : 3 ...