POJ2752 Seek the Name, Seek the Fame KMP应用
来源:互联网 发布:quicktime是什么软件 编辑:程序博客网 时间:2024/05/24 01:16
POJ2752 Seek the Name, Seek the Fame
这算是一道比较脑洞的KMP题目。
先来看题意。对于给定的一个字符串,有一部分前缀和后缀是相同的,让你从小到大输出这些相同前缀后缀的长度。
如样例 ababcababababcabab,相同的前缀后缀有“ab”,”abab”,”ababcabab”,”ababcababababcabab”四个,长度分别为2 4 9 18;
然后来看一看解法:
对于一个字符串 s[0…n-1] ,假设 s[0…i] , s[j…n-1] 是相同的,那么所有比它小的前缀后缀,如 s[0…x] , s[y…n-1] 都可以放在s[0…i]中( s[0…x] , s[z…i] )。那么,我们就可以从n-1开始,从大到小找。
代码
#include <iostream>#include <stack>#include <cstring>using namespace std;char s[400005];int next[400005];stack <int> st;void get_next(int len){ // 求next数组 int i = 0, j = -1; next[0] = j; while(i != len){ if(j == -1 || s[i] == s[j]) next[++i] = ++j; else j = next[j]; }}int main(){ while(cin >> s){ int l = strlen(s); get_next(l); int i = l; while(i != 0){ // 从长度l开始,直到i == 0为止 st.push(i);//把答案放在栈中,反向输出。 i = next[i]; } while(!st.empty()){//利用栈的特性,后入先出,即可满足题目要求 cout << st.top()<<' ';st.pop(); } cout << endl; } return 0;}
0 0
- POJ2752 Seek the Name, Seek the Fame KMP应用
- 【KMP】 poj2752 Seek the Name, Seek the Fame
- poj2752 Seek the Name, Seek the Fame--------KMP
- poj2752 Seek the Name, Seek the Fame KMP
- POJ2752 Seek the Name, Seek the Fame 【KMP】
- POJ2752 Seek the Name, Seek the Fame KMP算法
- POJ2752 Seek the Name, Seek the Fame【KMP】
- POJ2752 Seek the Name, Seek the Fame(KMP)
- POJ2752 Seek the Name, Seek the Fame(kmp)
- 【poj2752】Seek the Name, Seek the Fame(KMP)
- 【poj2752】Seek the Name, Seek the Fame KMP
- POJ2752 Seek the Name, Seek the Fame KMP
- poj2752 Seek the Name, Seek the Fame (kmp)
- kmp算法 poj2752 Seek the Name, Seek the Fame
- 【POJ2752】Seek the Name, Seek the Fame-KMP思想
- kmp杂题3 poj2752 Seek the Name, Seek the Fame
- 【POJ2752】Seek the Name, Seek the Fame(KMP)
- poj2752 Seek the Name, Seek the Fame(KMP)
- Excel 取消科学记数法显示
- 在线计算小工具
- 基础总结篇之五:BroadcastReceiver应用详解
- [原创]纯java实现http web服务器
- CSDN-markdown编辑器使用说明
- POJ2752 Seek the Name, Seek the Fame KMP应用
- Android studio 修复编译MaxPermSize参数无法使用
- 在iOS开发中,在视频初始状态时获取视频的第一帧图片
- 正则表达式学习笔记
- iOS——NSTimer的使用,计时器的简单创建
- 安卓四大控件之BroadcastReceiver详解
- ScrollView小知识点
- java ArrayList 细节
- PLSQL DEVELOPER 使用的一些技巧【转】