HDU 1358 Period(kmp简单解决)
Period
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3196 Accepted Submission(s): 1603
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 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.
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.
#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简单解决)的更多相关文章
- HDU 1358 Period KMP
题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1358 求周期问题,简单KMP—— AC代码: #include <iostream> # ...
- hdu 1358 period KMP入门
Period 题意:一个长为N (2 <= N <= 1 000 000) 的字符串,问前缀串长度为k(k > 1)是否是一个周期串,即k = A...A;若是则按k从小到大的顺序输 ...
- HDU 1358 Period(KMP计算周期)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1358 题目大意:给你一串字符串,判断字符串的前缀是否由某些字符串多次重复而构成. 也就是,从第1个字母 ...
- Hdu 1358 Period (KMP 求最小循环节)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1358 题目描述: 给出一个字符串S,输出S的前缀能表达成Ak的所有情况,每种情况输出前缀的结束位置和 ...
- hdu 1358 Period(KMP入门题)
Period Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
- HDU 1358 Period(KMP next数组运用)
Period Problem Description For each prefix of a given string S with N characters (each character has ...
- [HDU 1358]Period[kmp求周期]
题意: 每一个power前缀的周期数(>1). 思路: kmp的next. 每一个前缀都询问一遍. #include <cstring> #include <cstdio> ...
- HDU 1358 Period (kmp求循环节)(经典)
<题目链接> 题目大意: 意思是,从第1个字母到第2字母组成的字符串可由某一周期性的字串(“a”) 的两次组成,也就是aa有两个a组成: 第三行自然就是aabaab可有两个aab组成: 第 ...
- 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 ...
随机推荐
- Android 卡顿优化 4 布局优化实际技巧
今天分享一些layout布局书写中的一些技巧,希望看过之后你也一样可以写出性价比高的布局.我个人的目标是用最少的View写出一样效果的布局.因为我相信View的数量减少伴随着的就是层级的减少.从而达到 ...
- iOS:在使用Cocoapods安装shareSDK时出现的link路径错误
CocoaPods 错误 target overrides the `OTHER_LDFLAGS`... Xcode 升级到 6.0 后,更新 CocoaPods,出现了如下的警告 [!] The ` ...
- WinForm客户端调用 WebService时 如何启用Session
WinForm客户端调用 WebService时 如何启用Session 摘自: http://www.cnblogs.com/swtseaman/archive/2011/04/18/2020176 ...
- java 过滤器(自己的理解)
filter继承javax.servlet.* 必须实现doFilter方法 chain.doFilter(request, response);这句话必须写在doFilter方法内部(以便调用其他的 ...
- centos/7/isos/x86_64 下载
为了节约有限的可用带宽. 不从mirror.centos.org下载iso映像 以下镜子应该可用的ISO映像: http://mirrors.aliyun.com/centos/7/isos/x86_ ...
- python __slot__
class Player(object): def __init__(self,name,age,life): self.name=name self.age=age self.life=life c ...
- org.w3c.dom(java dom)解析XML文档
位于org.w3c.dom操作XML会比较简单,就是将XML看做是一颗树,DOM就是对这颗树的一个数据结构的描述,但对大型XML文件效果可能会不理想 首先来了解点Java DOM 的 API:1.解析 ...
- 算法笔记_158:算法提高 逆序排列(Java)
目录 1 问题描述 2 解决方案 1 问题描述 问题描述 编写一个程序,读入一组整数(不超过20个),并把它们保存在一个整型数组中.当用户输入0时,表示输入结束.然后程序将把这个数组中的值按逆序重新存 ...
- mysql 取字段内容的第一个字符并大写
update words set `indexkey` = UPPER(left(word,1)) mysql 取字段内容的第一个字符并大写 用到两个mysql函数: 转换为大写:upper( ) 截 ...
- [物理题+枚举] hdu 4445 Crazy Tank
题意: 给你N个炮弹的发射速度,以及炮台高度H和L1,R1,L2,R2. 问任选发射角度.最多能有几个炮弹在不打入L2~R2的情况下打入L1~R1 注意:区间有可能重叠. 思路: 物理题,发现单纯的依 ...