HDOJ1358 Period 【KMP next数组应用】
来源:互联网 发布:显示屏改字软件 编辑:程序博客网 时间:2024/05/20 18:43
题目描述
n个字符的字符串,从第二个字符开始遍历。如果从第一个字符到当前字符是有循环的,那么输出当前的位置和循环次数。两组数据之间输出一个空格。
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
解题思路
假设当前是第i个字符,那么next[i+1]就是前i个字符的串里前后最大公共串,用i-next[i+1]+1就是除了前面的公共串剩下的一部分,这一部分如果可以被(i+1)整除,就一定能够有循环节。
下面是代码
#include <cstdio>#include <cstring>const int maxn = 1000010;char s[maxn];int next[maxn];int n,kase = 0;int len;void getNext(void){ memset(next,-1,sizeof(next)); int k = -1; int p = 0; next[p] = k; while(p <= len-1) { if(k == -1 || s[p] == s[k]) { p++;k++; next[p] = k; }else k = next[k]; }}int main(){ while(scanf("%d",&n) && n) { memset(s,0,sizeof(s)); scanf("%s",s); len = strlen(s); getNext(); printf("Test case #%d\n",++kase); for(int i = 1 ; i < len ; i ++) { if(next[i+1] == 0) continue; int l = i+1 - next[i+1]; if((i+1)%l == 0) printf("%d %d\n",i+1,(i+1)/l); } printf("\n"); } return 0;}
1 0
- HDOJ1358 Period 【KMP next数组应用】
- HDOJ1358 Period (KMP next数组应用)
- HDOJ1358 Period(kmp)
- Period next数组应用
- POJ 1961 Period KMP(next数组应用)
- HDOJ 1358.Period(KMP中next数组应用)
- POJ 1961 Period KMP next数组的应用
- [kmp算法next数组的应用][poj1961]Period
- HDU - 1358 Period(KMP next数组)
- Period(kmp简单的应用 next从零开始 )
- Poj-1961 Period(next数组应用)
- 【next数组应用】HDU 1358 Period
- KMP及其next数组性质学习小记 Poj1961 Period
- 1358 Period hdu kmp的next数组运用
- POJ 1961 Period(KMP next数组巧用)
- HDOJ 1358 Period(KMP next数组运用)
- FZU - 1901 Period II(KMP next数组的理解)
- poj 1961 Period(未优化KMP的next数组)
- js设置页面定时刷新及页面跳转
- 查看mysql错误日志定位mysql错误 .
- 关于struts2文件上传中的uploadContentType和uploadFileName
- IOS开发之我对block的认识总结
- 常用元器件的封装
- HDOJ1358 Period 【KMP next数组应用】
- 手机尾号hash算法
- C++拾遗--智能指针
- The connection to adb is down, and a severe error has occured.please...
- Oracle数据库中truncate命令和delete命令的区别
- cuda-convnet2与caffe对比
- Java JDBC批处理插入数据操作
- plsql配置登录连接
- 杭电2022