POJ - 2752 Seek the Name, Seek the Fame (next 数组的应用)
2017-10-18 20:44
399 查看
https://cn.vjudge.net/contest/189927#problem/C
题意:就是问你前后缀相等的长度,从小到大去输出。
思路:nex数组里面存的就是当前的相等的前后缀长度,最长的相等前后缀就是他自己,之后的想澄清前后缀就是nex[len],之后一直输出直到nex[len]为0 就好了上代码:#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
char ch[400050];
int nex[400050],ans[400050];
void pre_kmp()
{
memset(nex , 0 ,sizeof(nex));
int len = strlen(ch) , k = -1 ,i = 0;
nex[0]=-1;
while(i < len)
{
if (k == -1 || ch[i] == ch[k])
{
i++;
k++;
nex[i]=k;
}
else k = nex[k];
}
}
int main()
{
while(scanf("%s",ch)!=EOF)
{
pre_kmp();
int cnt = 0;
int p = strlen (ch);
while(p>=1)
{
ans[cnt++]=p;
p = nex[p];
}
for(int i = cnt -1 ;i >=0 ; i--)
{
if(i == cnt-1)
{
printf("%d",ans[i]);
}
else
printf(" %d",ans[i]);
}
puts("");
}
return 0;
}
题意:就是问你前后缀相等的长度,从小到大去输出。
思路:nex数组里面存的就是当前的相等的前后缀长度,最长的相等前后缀就是他自己,之后的想澄清前后缀就是nex[len],之后一直输出直到nex[len]为0 就好了上代码:#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;
char ch[400050];
int nex[400050],ans[400050];
void pre_kmp()
{
memset(nex , 0 ,sizeof(nex));
int len = strlen(ch) , k = -1 ,i = 0;
nex[0]=-1;
while(i < len)
{
if (k == -1 || ch[i] == ch[k])
{
i++;
k++;
nex[i]=k;
}
else k = nex[k];
}
}
int main()
{
while(scanf("%s",ch)!=EOF)
{
pre_kmp();
int cnt = 0;
int p = strlen (ch);
while(p>=1)
{
ans[cnt++]=p;
p = nex[p];
}
for(int i = cnt -1 ;i >=0 ; i--)
{
if(i == cnt-1)
{
printf("%d",ans[i]);
}
else
printf(" %d",ans[i]);
}
puts("");
}
return 0;
}
相关文章推荐
- POJ 2752 KMP算法中next数组的应用
- POJ 2406 Power Strings (KMP next数组应用)
- POJ 1961 Period KMP算法next数组的应用
- poj 2406 Power Strings (kmp 中 next 数组的应用||后缀数组)
- POJ-2752-Seek the Name, Seek the Fame(KMPnext数组应用)
- poj2752_Seek the Name,Seek the Fame_KMP(next数组定义的应用)
- poj2752—kmp的next数组的简单应用
- Poj-1961 Period(next数组应用)
- POJ 2752: S 4000 eek the Name, Seek the Fame(简单KMP-NEXT数组的应用)
- POJ 2752 Seek the Name, Seek the Fame(KMP next数组应用)
- poj 2752 next数组的应用
- POJ 2752 Seek the Name, Seek the Fame(KMP,next数组的应用)
- POJ-Power Strings(next数组应用)
- POJ 2406 Power Strings 1961的简化版,kmp的next数组的应用
- POJ 2406 Power Strings(next数组应用)
- poj 2752 next数组的应用
- poj 2752 kmp(next数组的应用)
- POJ 2572(Seek the Name, Seek the Fame) KMP中next数组的应用
- poj 2406 Power Strings KMP的next数组的应用
- poj 1961 Period (kmp next 数组的应用)