题意:给你一个字符串,寻找前缀和后缀相同的子串(包括原串)、 从小到大排列输出其子串的长度

思路:KMP  next 数组应用、

其实就是一个数学推导过程、

首先由next数组 可知s(ab) = s(bd)   此时next[d]=b   而此时 next[b]=f,意味着s(ef)=s(gh)

 s(gh)属于s(ab)   s(ab)=s(bd);   所以可以推出 s(gh)属于s(bd)    且s(ef)=s(gh)  所以 s(ef)属于s(cd)

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
const int qq=400000+10;
char node[qq];
int next[qq];
int get[qq];
int len;
void getnext()
{
int i,j;
i=0;j=-1;
next[0]=-1;
while(i<len){
if(j==-1||node[i]==node[j])
next[++i]=++j;
else
j=next[j];
}
return;
}
int main()
{
while(~scanf("%s",node)){
len=strlen(node);
getnext();
int k=len;
int i=0;
while(next[k]!=0){ // 那个公式的推导体现在这里、
get[i++]=next[k];
k=next[k];
}
for(int j=i-1;j>=0;--j)
printf("%d ",get[j]);
printf("%d\n",len);
}
return 0;
}

POJ 2752 Seek the Name, Seek the Fame next数组理解加深的更多相关文章

  1. (KMP)Seek the Name, Seek the Fame -- poj --2752

    http://poj.org/problem?id=2752 Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536 ...

  2. Seek the Name, Seek the Fame POJ - 2752

    Seek the Name, Seek the Fame POJ - 2752 http://972169909-qq-com.iteye.com/blog/1071548 (kmp的next的简单应 ...

  3. KMP POJ 2752 Seek the Name, Seek the Fame

    题目传送门 /* 题意:求出一个串的前缀与后缀相同的字串的长度 KMP:nex[]就有这样的性质,倒过来输出就行了 */ /************************************** ...

  4. POJ 2752 Seek the Name, Seek the Fame [kmp]

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

  5. poj 2752 Seek the Name, Seek the Fame(KMP需转换下思想)

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

  6. poj 2752 Seek the Name, Seek the Fame【KMP算法分析记录】【求前后缀相同的子串的长度】

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

  7. POJ 2752 Seek the Name, Seek the Fame(next数组运用)

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

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

    Seek the Name,Seek the Fame 过了个年,缓了这么多天终于开始刷题了,好颓废~(-.-)~ 我发现在家真的很难去学习,因为你还要陪父母,干活,做家务等等 但是还是不能浪费时间啊 ...

  9. poj 2752 Seek the Name, Seek the Fame (KMP纯模版)

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

随机推荐

  1. Hdu 1156

    题目链接 Brownie Points II Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java ...

  2. JS---案例:旋转木马

    案例:旋转木马 页面加载时候出现的效果,script标签写在head里面,body上面 显示一个图片散开的动画,遍历之后,把每个图片用封装的动画函数移动到指定目标(同时改变多属性:宽,透明度,层级,t ...

  3. GDSOI2017第三轮模拟4.21 总结

    1 第一题看着就觉得猎奇,于是就想着打暴力就跑. 但是很严重的问题就是... \(D\)和\(B\)打反了,都不知道当时在干什么??? 原本可以拿35. 2 第二题看着就觉得套路,于是想着今天就攻这题 ...

  4. temp for @青

    4层方法 IBaseController  BaseControllerImpl IBaseService BaseServiceImpl  IBaseComponent   IBaseCompone ...

  5. DirectX11笔记(一)--配置DirectX工程

    原文:DirectX11笔记(一)--配置DirectX工程 版权声明:本文为博主原创文章,未经博主允许不得转载. https://blog.csdn.net/u010333737/article/d ...

  6. Direct2D 第3篇 绘制文字

    原文:Direct2D 第3篇 绘制文字 #include <windows.h> #include <d2d1.h> #include <d2d1helper.h> ...

  7. javascript里的eval总结

    JavaScript eval() 函数 1.定义和用法 eval() 函数计算 JavaScript 字符串,并把它作为脚本代码来执行. 如果参数是一个表达式,eval() 函数将执行表达式.如果参 ...

  8. python ndarray相关操作:索引

  9. 使用R拟合分布

    使用R拟合分布 几个常用的概率函数介绍 这里,参考R语言实战,以及[Fitting Distribution with R]的附录. 一.认识各种分布的形态 1.1 连续型随机变量的分布 首先,我们来 ...

  10. thinkphp5.0 使用action()报Cannot redeclare app\home\controller\CheckSubstrs()错误

    原因:Common公共类方法isMobile()内部定义了函数CheckSubstrs(),在使用action()时,会调用两次isMobile(),导致函数CheckSubstrs()重复定义 解决 ...