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 ...
随机推荐
- ECCV 2014 Oral Paper
今年的ECCV 的paper已经挂到了网上.本文整了当中Oral Paper. 一,Tracking and Activity Recognition
- leetCode(45):Min Stack
Design a stack that supports push, pop, top, and retrieving the minimum element in constant time. pu ...
- phpstorm不安装apache就可以本地测试PHP
最近再搞个PHP的项目,找了很多发现phpstorm这个非常小巧而且很好用,,顺便推荐一下idea开发android非常不错,这2个IDE都是一家公司的.本文由智动软件(zdexe.com)原创,转载 ...
- STL容器简介
stl不是面向对象的编程,而是一种不同的编程模式————泛型编程 我们常用到的STL容器有vector.list.deque.map.multimap.set.multiset 顺序性容器:vecto ...
- ORA-04030
ORA-04030: 在尝试分配...字节(...)时进程内存不足的原因分析及解决办法 正在使用的oracle 11g数据库,前天在用一段时间后(开始要较长时间才出现,后来较短时间就出现),频繁报OR ...
- [CF 276C]Little Girl and Maximum Sum[差分数列]
题意: 给出n项的数列A[ ], q个询问, 询问 [ l, r ] 之间项的和. 求A的全排列中该和的最大值. 思路: 记录所有询问, 利用差分数列qd[ ], 标记第 i 项被询问的次数( 每次区 ...
- Maven C盘用户文件下没有.m2
在配置好Maven的环境之后,先运行一条命令: mvn help:system 该命令会打印出所有的Java系统属性和环境变量. 运行这条命令的目的是让Maven执行一个真正的任务.可以从命令行上看到 ...
- mysql 比较隐秘的问题
2017-11-13 13:47:27:DEBUG DubboServerHandler-192.168.30.114:20990-thread-5 com.yryz.qshop.modules.in ...
- 搭建dubbo-admin-2.5.3
dubbo管理界面 一,安装zookeeper 1,下载包zookeeper-3.3.3.tar.gz 2,解压 tar zxvf zookeeper-3.3.3.tar.gz cd zookeepe ...
- Lina Group
var query = from ll in proListRequestList group ll by new {ll.pro_id} into g select new ProListReque ...