poj_2752_kmp_nxt_application_求s 所有子串是s的前缀又是后缀的长度
来源:互联网 发布:互联网职位 知乎 编辑:程序博客网 时间:2024/05/21 06:42
Step1. Connect the father's name and the mother's name, to a new string S.
Step2. Find a proper prefix-suffix string of S (which is not only the prefix, but also the suffix of S).
Example: Father='ala', Mother='la', we have S = 'ala'+'la' = 'alala'. Potential prefix-suffix strings of S are {'a', 'ala', 'alala'}. Given the string S, could you help the little cat to write a program to calculate the length of possible prefix-suffix strings of S? (He might thank you by giving your baby a name:)
Input
Restrictions: Only lowercase letters may appear in the input. 1 <= Length of S <= 400000.
Output
Sample Input
ababcababababcababaaaaa
Sample Output
2 4 9 181 2 3 4 5
题意:
给你字符串s ,让你求所有子串即使s的前缀又是后缀的长度,从小到大输出
解;
盗图:
如左图,假设黑色线来代表字符串str,其长度是len,红色线的长度代表next[len],根据next数组定义易得前缀的next[len]长度的子串和后缀next[len]长度的子串完全相同(也就是两条线所对应的位置)。我们再求出next[len]位置处的next值,也就是图中蓝线对应的长度。同样可以得到两个蓝线对应的子串肯定完全相同,又由于第二段蓝线属于左侧红线的后缀,所以又能得到它肯定也是整个字符串的后缀。
所以对于这道题,求出len处的next值,并递归的向下求出所有的next值,得到的就是答案。
#include<iostream>#include<cstdio>#include<stack>#include<cstring>using namespace std;const int N=(int)4e5+10;int nxt[N],m;void get(char *s){ int i=0,j=-1; nxt[0]=-1; while(i<m) { if(j==-1||s[i]==s[j]) nxt[++i]=++j; else j=nxt[j]; }} char s[N];int main(){ while(cin>>s) { m=strlen(s); get(s); stack<int > q; while(!q.empty()) { q.pop();} q.push(m); int j=m; while(j) { q.push(nxt[j]); j=nxt[j]; } while(!q.empty()) { int tt=q.top(); if(tt) cout<<tt<<" "; q.pop(); } puts(""); } return 0;}
- poj_2752_kmp_nxt_application_求s 所有子串是s的前缀又是后缀的长度
- poj 2752 (找既是前缀又是后缀的串的长度)kmp/拓展kmp
- 求所有前缀-后缀字符串相等的长度(kmp算法中的nxet数组)
- 【Kmp求既是前缀又是后缀的子串】POJ
- POJ2752 既是前缀又是后缀的子串
- 所有既是前缀又是后缀的子串 KMP POJ 2752 Seek the Name, Seek the Fame
- 计蒜客 Barty's Computer hash求字符串前缀和后缀
- poj 2752 Seek the Name, Seek the Fame 【在原串中 找所有既是原串前缀又是原串后缀的子串长度】
- POJ 2752 既是前缀又是后缀的子串 (kmp)
- POJ2752 Seek the Name, Seek the Fame 【既是前缀又是后缀的子串】
- 后缀s是如何影响标志位的
- HDU4691用RMQ求所有字符串后缀的最长公共前缀
- POJ 2752 既是前缀又是后缀
- B/S项目结束,又是一个新的开始
- 求 数组里 和为s的所有组合
- CF432D 求等于原串前缀的后缀子串,输出子串的长度和出现次数
- 陈利人 面试题 给定两个字符串s和t(len(s)>len(t)),t可能是s的一个子序列。求所有s的子序列集合中,有多少个子序列等于t。
- 陈利人 面试题 给定两个字符串s和t(len(s)>len(t)),t可能是s的一个子序列。求所有s的子序列集合中,有多少个子序列等于t。
- Linux学习——Linux常用命令-文件处理命令
- XSHELL工具上传文件到Linux以及下载文件到本地(Windows)
- spring boot 部署
- LintCode刷题——最长上升连续子序列
- code profiling
- poj_2752_kmp_nxt_application_求s 所有子串是s的前缀又是后缀的长度
- caffe中用faster-rcnn训练自己的数据集中出现的各种问题总结
- scrapy学习
- 检验样本是否服从正态分布,处理偏态分布
- Hibernate(2)
- 必须要见识
- 活动记录
- iOS delegate的优势及缺点
- 定长顺序表