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 ...
随机推荐
- AS3.0 Vector的运用
使用Vector类编程 一个array(数组)就像是把一套变量组织在一起的容器.单个数组可以含有许多不同的值.你可以储存和取得数组中的单个值(也就是数组elements(元素)).你也可以通过直接操作 ...
- 新公司官网项目优化实践(Vue)
入职后接手website-html和website-mobile项目,发现项目加载速度不太理想,于是结合自己之前的经验对项目做了优化.此篇文章主要记录这次优化详情. 原始项目:开发环境:website ...
- 理解Flow静态类型检查
一.为什么在JavaScript中使用静态类型 了解静态类型的最快方法是将其与动态类型进行对比. 有静态类型参数的语言被称为静态类型语言. 另一方面,有动态类型参数的语言被称为动态类型语言.核心区别是 ...
- PhantomJS + Selenium webdriver 总结-元素定位
webdriver提供了丰富的API,有多种定位策略:id,name,css选择器,xpath等,其中css选择器定位元素效率相比xpath要高些,使用id,name属性定位元素是最可靠,效率最高的一 ...
- Kudu – 在快数据上的进行快分析的存储
转自: http://www.tuicool.com/articles/nmYf2uf Cloudera Impala Kudu – 在快数据上的进行快分析的存储 Kudu,对应中文的含义应该 ...
- SqlBulkCopy-从 bcp 客户端收到一个对 colid 1 无效的列长度
经过研究,问题是因为tatatable中的colid列1的数据字段长度超过了数据表中定义的字段长度. 解决方法就是将数据库该字段的长度增大问题就解决了.
- Linux下libsvm的安装及简单练习
引文:常常在看paper的时候.就看到svm算法,可是要自己来写真的是难于上青天呀! 所幸有一个libsvm的集成软件包给我们使用,这真的是太好了.以下简介下怎么来使用它吧! LIBSVM是一个集成软 ...
- automake连载---关于两个文件configure.in和Makefile.am的编写
http://blog.csdn.net/shanzhizi/article/details/30251763 automake主要通过编辑Makefile.am来控制它的行为,下面就常用的三个Mak ...
- vue - 条件语句
1.与小程序不同之处一,小程序无论变量还是常亮都可以用双向绑定来解决{{}},而vue一旦双(单)引号包起来以后就失效了. 2.注意一点,切记双引号注意不要混淆哈,这里是一排双引号包单引号,那里是一排 ...
- EventBus的粘性事件
下午赶去公司解决了电台业务首次语音搜台后(用到服务,但只出一个独立的Activity,主界面并没有打开)不能听歌识曲的问题. 排查到最后,去识别的消息确实是发出去了,但是却没有收到,没有收到消息当然不 ...