需要理解next[]的意义。之前看到大牛的博客,next[]讲的非常清楚。

利用next[],当前位子的前面那一段和next[当前位子]的前面那一段是相同的。又next[next[当前位子]]与next[当前位子]有一段相同,所以当前位子的前面一段和

next[next[当前位子]]的前一段有相同即可。

#include<stdio.h>
#include<string.h>
#include<stack>
using namespace std;
#define maxn 400010
int next[maxn];
char s[maxn];
void getnext()
{
int j,k,len=strlen(s);
next[]=-;
j=;
k=-;
while(j<len)
{
if(k==-||s[j]==s[k])
{
j++;
k++;
next[j]=k;
}
else k=next[k];
}
}
int main()
{
int i,j,flag;
while(scanf("%s",s)!=EOF)
{
flag=;
stack<int>sta;
getnext();
i=strlen(s);
sta.push(i);
while(next[i]!=)
{
sta.push(next[i]);
i=next[i];
}
while(!sta.empty())
{
int ans=sta.top();
sta.pop();
if(flag==)
{
flag=;
printf("%d",ans);
}
else printf(" %d",ans);
}
printf("\n");
}
}

poj2752 KMP的更多相关文章

  1. POJ-2752(KMP算法+前缀数组的应用)

    Seek the Name, Seek the Fame POJ-2752 本题使用的算法还是KMP 最主要的片段就是前缀数组pi的理解,这里要求解的纸盒pi[n-1]有关,但是还是需要使用一个循环来 ...

  2. POJ2752 Seek the Name, Seek the Fame —— KMP next数组

    题目链接:https://vjudge.net/problem/POJ-2752 Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Li ...

  3. POJ2752 Seek the Name,Seek the Fame KMP算法

    KMP继续练手.题目问的是一个串前缀等于后缀的可能长度是哪些,输出来.题目考的是对KMP失配指针的理解,当最后一位失配(即'\0'那里)时,指针会移动到前缀对应匹配的部分,所以这个长度是我们要的,然后 ...

  4. 【POJ2752】【KMP】Seek the Name, Seek the Fame

    Description The little cat is so famous, that many couples tramp over hill and dale to Byteland, and ...

  5. poj-2752(拓展kmp)

    题意:求一个串所有的前后缀字串: 解题思路:kmp和拓展kmp都行,个人感觉拓展kmp更裸一点: 拓展kmp: #include<iostream> #include<algorit ...

  6. POJ-2752 Seek the Name, Seek the Fame 字符串问题 KMP算法 求前后缀串相同数木

    题目链接:https://cn.vjudge.net/problem/POJ-2752 题意 给一个字符串,求前缀串跟后缀串相同的前缀串的个数 例:alala 输出:a, ala, alala 思路 ...

  7. KMP——POJ-3461 Oulipo && POJ-2752 Seek the Name, Seek the Fame && POJ-2406 Power Strings && POJ—1961 Period

    首先先讲一下KMP算法作用: KMP就是来求在给出的一串字符(我们把它放在str字符数组里面)中求另外一个比str数组短的字符数组(我们叫它为ptr)在str中的出现位置或者是次数 这个出现的次数是可 ...

  8. poj-------------(2752)Seek the Name, Seek the Fame(kmp)

    Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 11831   Ac ...

  9. POJ-2752 Seek the Name, Seek the Fame(KMP,前缀与后缀相等)

    题意:    给出一个字符串str,求出str中存在多少子串,使得这些子串既是str的前缀,又是str的后缀.从小到大依次输出这些子串的长度. 这个就是next数组的应用,next数组真是很深奥啊. ...

随机推荐

  1. 我的opencv之旅:ios人脸识别

    学习opencv有一年多了,这本来是我的毕业设计的一部分,但是因为不能突出专业重点,所以换了个课题. opencv在vc.android.ios下都能用,其中vc和android下的教程和主题贴最多, ...

  2. hdu-5904 LCIS(水题)

    题目链接: LCIS Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total ...

  3. HDU 2065 “红色病毒”问题 --指数型母函数

    这种有限制的类棋盘着色问题一般可以用指数型母函数来解决,设Hn表示这样的着色数,首先H0=1,则Hn等于四个字母的(A,B,C,D)的多重集合的n排列数,其中每个字母的重数是无穷,且要求A,C出现的次 ...

  4. YOU ARE MY SUNSHINE

    /*you are sunshine, my only sunshine, you make me happy when skies are grey. you'll never know dear ...

  5. ubuntu 命令收集

    1. ctrl + Alt + F1:   进入纯粹的命令行. 2. ctr + Alt + T :    从图形界面打开终端.

  6. Android签名机制:生成keystore、签名、查看签名信息

    转自:http://www.ourunix.org/post/146.html

  7. js Date日期对象的扩展

    // 对Date的扩展,将 Date 转化为指定格式的String// 月(M).日(d).小时(h).分(m).秒(s).季度(q) 可以用 1-2 个占位符, // 年(y)可以用 1-4 个占位 ...

  8. linux如何挂载windows下的共享文件

    说明:windows下有一共享文件夹APP,windows本地ip是192.168.9.155现在需要在linux服务器上挂载这个APP文件夹,linux服务器ip是192.168.9.200 操作记 ...

  9. sql 索引 填充因子(转)

    和索引重建最相关的是填充因子.当创建一个新索引,或重建一个存在的索引时,你可以指定一个填充因子,它是在索引创建时索引里的数据页被填充的数量.填充因子设置为100意味着每个索引页100%填满,50%意味 ...

  10. How to configure SRTM elevations in WorldWind WMS

    In this thread I will try to explain how to serve SRTM elevations using NASA WorldWind WMS. ! Import ...