POJ 2752 Seek the Name, Seek the Fame
来源:互联网 发布:淘宝运营模式图 编辑:程序博客网 时间:2024/06/05 07:31
给定一个字符串s,从小到大输出s中既是前缀又是后缀的子串的长度
从最后一位开始回溯,若s[next[n-1]] == s[n-1],则子串s[0,1,2,...,next[n-1]]是满足条件的子串。然后判断s[next[next[n-1]]] == s[n-1]是否成立,这样一直回滚,直到next[next[.....next[n-1]]] == -1停止,就可以算出所有符合条件的解
#include <iostream>#include <cstring>#include <cstdio>#include <algorithm>#include <vector>#define rep(i, j, k) for(int i = j; i <= k; i++)#define maxn 1000009using namespace std;int n, Next[maxn];char s[maxn];vector <int> ans;void pre (){Next[0] = -1;Next[1] = 0;rep (i, 1, n - 1){int j = Next[i];while (j && s[j] != s[i])j = Next[j];Next[i + 1] = s[i] == s[j] ? j + 1 : 0;}}void work (int x){if (Next[x] != -1){ans.push_back (x);work (Next[x]);}}int main (){while (scanf ("%s", s) == 1) {n = strlen (s);pre ();//rep (i, 0, n - 1) printf ("%d %c\n", i, s[i]); rep (i, 0, n) printf ("%d %d\n", i, Next[i]);ans.clear ();work (n);sort (ans.begin(), ans.end ());if (!ans.empty ()){printf ("%d", ans[0]);rep (i, 1, (int)ans.size () - 1)printf (" %d", ans[i]);}cout << endl;}return 0;}
0 0
- poj seek the name,seek the fame
- poj---Seek the Name, Seek the Fame
- Seek the Name, Seek the Fame POJ
- Seek the Name, Seek the Fame POJ
- POJ-2752 Seek the Name, Seek the Fame
- poj 2752 Seek the Name, Seek the Fame
- poj 2752 Seek the Name, Seek the Fame(KMP)
- POJ 2752--Seek the Name, Seek the Fame (next)
- POJ:2752Seek the Name, Seek the Fame
- POJ 2752 - Seek the Name, Seek the Fame
- poj 2752 Seek the Name, Seek the Fame
- poj 2752 Seek the Name, Seek the Fame
- POJ-2752-Seek the Name, Seek the Fame
- POJ 2752 Seek the Name, Seek the Fame
- poj 2752 Seek the Name, Seek the Fame
- poj 2752 Seek the Name, Seek the Fame
- POJ 2752 Seek the Name, Seek the Fame
- poj 2752 Seek the Name, Seek the Fame---kmp
- 【Ts 4】ftp服务器搭建
- POJ 4007 Flood-it!
- iOS App 项目目录结构的搭建
- 插入排序
- 算法Week02.04 - LeetCode 233. Number of Digit One
- POJ 2752 Seek the Name, Seek the Fame
- HDU1028-Ignatius and the Princess III
- 最佳实践(js dom编程艺术第五章笔记)
- 腾讯云Cengos 7.0 x64 javaweb环境搭建
- HDU 3807
- 数据结构的一些小知识(纯理论)
- String中对NULL和""的判断
- 音乐播放器
- SVN中trunk、branch、tag区别