Period kmp
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.
aaa
12
aabaabaabaab
0
2 2
3 3
Test case #2
2 2
6 2
9 3
12 4
#include<bits/stdc++.h>
using namespace std;
//input
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i>=(b);i--)
#define RI(n) scanf("%d",&(n))
#define RII(n,m) scanf("%d%d",&n,&m);
#define RIII(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define RS(s) scanf("%s",s);
#define ll long long
#define inf 0x3f3f3f3f
#define REP(i,N) for(int i=0;i<(N);i++)
#define CLR(A,v) memset(A,v,sizeof A)
//////////////////////////////////
#define N 1000000+5
int nex[N];
int lenp;
string p;
void getnext()
{
lenp=p.size();
nex[]=-;
int k=-,j=;
while(j<lenp)
{
if(k==-||p[j]==p[k])
nex[++j]=++k;
else k=nex[k];
}
} int main()
{
int n;
int cas=;
while(RI(n),n)
{
if(!n)break; printf("Test case #%d\n",++cas);
cin>>p;
getnext();
rep(i,,lenp)
{
if(nex[i]==)continue;
int j=i-nex[i];
if(i%j==)
{
printf("%d %d\n",i,i/j);
}
}
cout<<endl;
}
}
Period kmp的更多相关文章
- POJ 1961 Period( KMP )*
Period Time Limit: 3000MSMemory Limit: 30000K Total Submissions: 12089Accepted: 5656 Description For ...
- hdu oj Period (kmp的应用)
Period Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Sub ...
- hdu 1358 period KMP入门
Period 题意:一个长为N (2 <= N <= 1 000 000) 的字符串,问前缀串长度为k(k > 1)是否是一个周期串,即k = A...A;若是则按k从小到大的顺序输 ...
- LA3026 - Period(KMP)
For each prefix of a given string S with N characters (each character has an ASCII code between 97 a ...
- poj1961 Period kmp解决找字符串的最小循环节
/** 题目:poj1961 Period 链接:http://poj.org/problem?id=1961 题意:求从1到i这个前缀(2<=i<=N) ,如果有循环节(不能自身单独一个 ...
- HDU1358 Period —— KMP 最小循环节
题目链接:https://vjudge.net/problem/HDU-1358 Period Time Limit: 2000/1000 MS (Java/Others) Memory Lim ...
- 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 ...
- UVA 1328 - Period KMP
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=36131 题意:给出一个长度为n的字符串,要求找到一些i,满足说从1 ...
- POJ 1961 Period KMP算法next数组的应用
题目: http://poj.org/problem?id=1961 很好的题,但是不容易理解. 因为当kmp失配时,i = next[i],所以错位部分就是i - next[i],当s[0]...s ...
随机推荐
- 洛谷P3317 [SDOI2014]重建 [Matrix-Tree定理]
传送门 思路 相信很多人像我一样想直接搞Matrix-Tree定理,而且还过了样例,然后交上去一分没有. 但不管怎样这还是对我们的思路有一定启发的. 用Matrix-Tree定理搞,求出的答案是 \[ ...
- table中border-collapse的问题
在table中,如果设置了border-collapse: collapse;,边框会合并,这时你修改top或bottom的颜色,会有问题 解决办法是:border-collapse: separat ...
- MVVM 简介
转:https://objccn.io/issue-13-1/ 所以,MVVM 到底是什么?与其专注于说明 MVVM 的来历,不如让我们看一个典型的 iOS 是如何构建的,并从那里了解 MVVM: 我 ...
- Confluence 6 使用 Jira 管理用户
如果你已经使用了 Jira 来管理你的任务和 issue 的话,你可以选择将 Jira 和 Confluence 整合在一起,将用户管理集中到一个地方.你可以控制你 Jira 中的用户组是否具有使用 ...
- Confluence 6 编辑站点欢迎消息使用模板编辑器的小提示
站点欢迎消息是一个模板而不是一个页面,所以你需要使用模板编辑器来对你的消息进行编辑. 你可以和在你 Confluence 中其他页面中一样,在站点欢迎消息模板中添加文本,连接和宏.但是添加图片的话会有 ...
- ios蓝牙详解
最近这段时间在研究蓝牙,也研究了一段时间了现在在下面做个总结 1 其实蓝牙连接只要明白了整体原理,其实挺简单的 2 大部分情况下,手机作为中心管理者,而连接的设备被称为外设,外设的结构有点像一颗大树 ...
- Java语法基础常见疑惑解答8,16,17,21图片补充
8. 16. 17. 21
- java常见错误总结
1. 现象:将数组转为List后进行removeAll()操作,报java.lang.UnsupportedOperationException错误. 代码: /** * 获取标记ID * @retu ...
- jmeter BeanShell实例-----两个变量之间的断言对比
jmeter BeanShell实例-----两个变量之间的断言对比 在jmeter的中,断言没法对两个变量的进行对比后判断,只能使用Bean Shell断言来进行,总是有人来问怎么写呢.这里写一个简 ...
- AI学习吧-登录注册
登录注册注销 如果需要给表设置权限,没有登录就不可以查看,只需要在每个视图函数之前加上Auth_classes=[ ]即可! 增加两张表,做登录认证 #models.py #做登录验证 class U ...