HDU 1358 Period(KMP算法应用)
来源:互联网 发布:java代码运行流程图 编辑:程序博客网 时间:2024/05/20 17:07
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 A K , that is A concatenated K times, for some string A. Of course, we also want to know the period K.
3aaa12aabaabaabaab0
Test case #12 23 3Test case #22 26 29 312 4
【题解】
KMP算法应用题,题意是从序列首开始遍历,寻找相同子序列数大于等于2的序列终点位置和相同子序列的个数,比如样例1就是i=2时有两个相同的子序列a,a,序列i=3时有三个想通过的子序列a,a,a。
直接套用KMP模板,中间判断一下,如果符合条件就输出。
ps:这道题我实在virtual judge 上交的,ce了好几次,这个平台上不支持数组名与关键字相同,就是说,在代码中用next做数组名在电脑上可以运行,但是交上去会ce,所以稍微改一下数组名就好了。
【AC代码】
#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>using namespace std;const int N=1e6+10;int next_t[N];string s;int m,n;void get_next(){ int k=-1,i=0; next_t[0]=-1; while(i<s.size()) { if(k==-1 || s[i] == s[k]) { k++; i++; if(i%(i-k)==0 && i/(i-k)>1) { printf("%d %d\n",i,i/(i-k)); } next_t[i]=k; } else k=next_t[k]; }}int main(){ int cnt=1; while(~scanf("%d",&m),m) { cin>>s; printf("Test case #%d\n",cnt++); get_next(); cout<<endl; } return 0;}
阅读全文
0 0
- HDU 1358 Period(KMP算法应用)
- hdu 1358 Period KMP算法
- hdu 1358 Period(KMP)
- hdu 1358 Period(KMP)
- HDU 1358 Period(KMP)
- HDU 1358 Period(kmp)
- HDU - 1358 - Period (KMP)
- HDU 1358 Period (KMP)
- hdu 1358 Period(KMP)
- 【hdu 1358】Period(kmp)
- HDU 1358 Period(KMP)
- hdu oj Period (kmp的应用)
- HDU 1358 Period (KMP)
- HDU 1358 Period KMP
- hdu 1358 Period KMP
- hdu 1358 Period(KMP)
- HDU 1358 Period KMP
- 【HDU】1358 Period KMP
- MYSQL 存储过程与自定义函数的区别
- linux下yum命令出现Loaded plugins: fastestmirror
- com.tencent.qcload.playersdk.ui.VideoRootFrame混淆报错问题
- RactNative---react-native-storage使用
- 将n个相同小球放入m个不同盒子内的放法种数
- HDU 1358 Period(KMP算法应用)
- echart 入门学习
- scala中的"unbound placeholder parameter"中的一种情况
- 数据结构实验之图论八:欧拉回路
- React 3 react component 3种写法
- Android设计模式之(4)----工厂方法模式
- 基于scrapyd爬虫发布总结
- Java中的String为什么是不可变的?—— String源码分析
- POJ-2528 Mayor's posters (离散化, 线段树)