HDU:1358-Period
Period
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/32768 K (Java/Others)
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
解题心得:
- 题意就是叫你找给出的字符串的每一个前缀中存在的循环节,其实很简单,就是在建立next数组的时候一边匹配,一边寻找循环节。裸找循环节。
#include<stdio.h>
#include<cstring>
using namespace std;
const int maxn = 1e6+100;
char s[maxn];
int next[maxn],n,t;
void cal_next()
{
memset(next,-1,sizeof(next));
next[0] = -1;
int k = -1;
for(int i=1;i<n;i++)
{
while(k > -1 && s[i] != s[k+1])
k = next[k];
if(s[i] == s[k+1])
k++;
next[i] = k;
if(k == -1)//当是-1的时候肯定没有循环节
continue;
int len = i+1;
if(len%(i-k) == 0)
printf("%d %d\n",len,len/(i-k));
}
printf("\n");
}
int main()
{
t = 1;
while(scanf("%d",&n) && n)
{
scanf("%s",s);
printf("Test case #%d\n",t++);
cal_next();
}
}
HDU:1358-Period的更多相关文章
- HDU 5908 Abelian Period (BestCoder Round #88 模拟+暴力)
HDU 5908 Abelian Period (BestCoder Round #88 模拟+暴力) 题目链接http://acm.hdu.edu.cn/showproblem.php?pid=59 ...
- hdu 1358 Period 最小循环节
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1358 分析:已知字符串,求其由最小循环节构成的前缀字符串. /*Period Time Limit: ...
- 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 ...
- hdu 1358 Period
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1358 思路:Next数组的用法,在第i个位置上如果有i%(i-Next[i])==0的话最小循环节就是 ...
- HDU 1358 Period KMP
题目地址: http://acm.hdu.edu.cn/showproblem.php?pid=1358 求周期问题,简单KMP—— AC代码: #include <iostream> # ...
- HDU 1358 Period(KMP计算周期)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1358 题目大意:给你一串字符串,判断字符串的前缀是否由某些字符串多次重复而构成. 也就是,从第1个字母 ...
- KMP + 求最小循环节 --- HDU 1358 Period
Period Problem's Link: http://acm.hdu.edu.cn/showproblem.php?pid=1358 Mean: 给你一个字符串,让你从第二个字符开始判断当前长度 ...
- Hdu 1358 Period (KMP 求最小循环节)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1358 题目描述: 给出一个字符串S,输出S的前缀能表达成Ak的所有情况,每种情况输出前缀的结束位置和 ...
- HDU 1358 Period (kmp判断循环子串)
Period Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Submis ...
- HDU 1358 Period 求前缀长度和出现次数(KMP的next数组的使用)
Period Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Subm ...
随机推荐
- ubuntu下编译安装mysql记录
搞了整整一天,好不容易折腾完,在此记录下,下次就省事了. 去官网http://www.php.net/downloads.php下载所需要的php版本,这里我选择5.6.22. ...
- [转]ubuntu16.04安装teamviewer12依赖包解决
安装teamviewer下载地址:http://www.teamviewer.com/en/download/linux/ 下载的是:teamviewer_12.0.76279_i386.deb ...
- zabbix文档3.4
zabbix 文档 3.4 5.zabbix 快速入门 1 登录和配置用户 用户名 : Admin 密 码 : zabbix 防止暴力袭击 在连续五次失败登录尝试的情况下,Zabbix界面将暂停30秒 ...
- javascript字符串格式化string.format
String.prototype.format = function () { var values = arguments; return this.replace(/\{(\d+)\}/g, fu ...
- nvm安装nodejs(安装在非系统盘内)
在使用nodejs时有时需要不同的版本之间进行切换,所以就用到了版本管理工具nvm,在windows系统下用的是nvm-windows,这里选择的是nvm-noinstall.zip免安装版本(需要配 ...
- vue+node+mongodb实现的页面
源代码地址:https://github.com/GainLoss/vue-node-mongodb 目前这个项目实现的是: 1.利用vue-cli实现前台页面的编写 (1)页面的跳转利用的是vue- ...
- Object comparison - (BOOL)isEqual:(id)other
https://developer.apple.com/library/content/documentation/General/Conceptual/DevPedia-CocoaCore/Obje ...
- 【BZOJ1216】[HNOI2003] 操作系统(堆+模拟)
点此看题面 大致题意: 有\(n\)个任务,每个任务有4个属性:编号.到达时间.执行时间和优先级.每个单位时间,会执行一个优先级最高(若有多个优先级最高的,就先执行到达时间较早的)的任务,请你按完成的 ...
- 【洛谷1110】[ZJOI2007] 报表统计(两棵平衡树)
点此看题面 大致题意: 有一个序列,三种操作.\(INSERT\)操作是在原数列第\(i+1\)个元素之前插入一个新元素\(k\),\(MIN\)\(GAP\)操作是查询相邻两个元素的之间差值的最小值 ...
- CPP-基础:C_C++变量命名规则
C_C++变量命名规则 变量命名规则是为了增强代码的可读性和容易维护性.以下为C++必须遵守的变量命名规则: 1. 变量名只能是字母(A-Z,a-z)和数字(0-9)或者下划线(_)组成. 2. 第一 ...