题目链接:http://poj.org/problem?id=2752
//pku2752
//题目意思:
//给一个串,找出所有前缀也是后缀的情况
#include<iostream>
#include<cstring>
using namespace std;
char str[400002];
int next[400002];
int result[400002];
int len;
void index()//用KMP算出next[1~len]
{
int i=0,j=-1;
next[0]=-1;
while(i<len)
{
if(j==-1||str[i]==str[j])//匹配
{
++i;++j;
next[i]=j;
}
else j=next[j];//j往回走
}
}
int main()
{
while(gets(str))
{
len=strlen(str);
int i=0,j=len;
index();
while(j!=0)
{
result[i++]=j;//从大到小记录可行情况
j=next[j];//j往回走
}
int k;
for(k=i-1;k>=0;k--)
{
cout<<result[k];
if(k>0)cout<<" ";
}
cout<<endl;
}
return 0;
}
分享到:
相关推荐
北大POJ1426-Find The Multiple【BFS+同余模】 解题报告+AC代码
北大POJ1027-The Same Game 解题报告+AC代码
北大POJ1163-The Triangle 解题报告+AC代码
北大POJ1163-The Triangle
POJ2635-The Embarrassed Cryptographer 测试数据。 来源:NCPC 2005 问题D
北大POJ2965-The Pilots Brothers' refrigerator 解题报告+AC代码
北大POJ3267-The Cow Lexicon
北大POJ2635-The Embarrassed Cryptographer 解题报告+AC代码
北大POJ3982-The Fibonacci sequence 解题报告+AC代码
poj 1611 The Suspects 代码 并查集的应用
北大POJ3267-The Cow Lexicon 解题报告+AC代码
poj 3548 Restoring the digits.md
poj 3554 Almost the shortest route.md
北大POJ1207-The 3n + 1 problem 解题报告+AC代码
北大POJ2151-Check the difficulty of problems 解题报告+AC代码
北大POJ2983-Is the Information Reliable【差分约束+优化Bellman】 解题报告+AC代码
北大POJ3239-Solution to the n Queens Puzzle 解题报告+AC代码
POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类POJ分类
北大POJ2388-Who's in the Middle 解题报告+AC代码
poj 1611 The Suspects.md