题意:
    给出一个字符串str,求出str中存在多少子串,使得这些子串既是str的前缀,又是str的后缀。从小到大依次输出这些子串的长度。

这个就是next数组的应用,next数组真是很深奥啊。

根据最后一个next数组的值,递归去找前面的值,直到是0时停止。证明见链接。

链接:http://www.cnblogs.com/dongsheng/archive/2012/08/13/2636261.html

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const int INF=0x3f3f3f3f;
typedef long long LL;
#define PI(A) printf("%d\n",A)
#define SI(N) scanf("%d",&(N))
#define SII(N,M) scanf("%d%d",&(N),&(M))
#define cle(a,val) memset(a,(val),sizeof(a))
#define rep(i,b) for(int i=0;i<(b);i++)
#define Rep(i,a,b) for(int i=(a);i<=(b);i++)
#define reRep(i,a,b) for(int i=(a);i>=(b);i--)
 ;

/*  /////////////////////////     C o d i n g  S p a c e     /////////////////////////  */

 +  ;

int nnext[MAXN];
char s[MAXN];
int sum[MAXN];
void getnext(int len)
{
    int i,j;
    i=;
    j=-;
    nnext[]=-;
    while(i<len)
    {
        ||s[i]==s[j])
        {
            ++i,++j;
            nnext[i]=j;
        }
        else
        {
            j=nnext[j];

        }
    }
}

int main()
{
    int len,i,k;
    while(scanf("%s",s)!=EOF)
    {
        k=;
        len=strlen(s);
        getnext(len);
        ;)
        {
            sum[k++]=nnext[i];
            i=nnext[i];
        }
        ; i>=; --i)
        printf("%d ",sum[i]);
        printf("%d\n",len);
    }
    ;
}

第二次,自己A

#include <map>
#include <set>
#include <list>
#include <cmath>
#include <queue>
#include <stack>
#include <vector>
#include <cstdio>
#include <string>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

const int INF=0x3f3f3f3f;
typedef long long ll;
#define PU puts("");
#define PI(A) printf("%d\n",A)
#define SI(N) scanf("%d",&(N))
#define SII(N,M) scanf("%d%d",&(N),&(M))
#define cle(a,val) memset(a,(val),sizeof(a))
#define rep(i,b) for(int i=0;i<(b);i++)
#define Rep(i,a,b) for(int i=(a);i<=(b);i++)
#define reRep(i,a,b) for(int i=(a);i>=(b);i--)
 ;

/*  /////////////////////////     C o d i n g  S p a c e     /////////////////////////  */

+ ;
int Next[MAXN];
int len;
char x[MAXN];
int  d[MAXN],cntd;

void kmp_pre()
{
    int i,j;
    j=Next[]=-;
    i=;
    while(i<len)
    {
        !=j&&x[i]!=x[j])j=Next[j];
        Next[++i]=++j;
    }
}

int main()
{
#ifndef ONLINE_JUDGE
    freopen("C:\\Users\\Zmy\\Desktop\\in.txt","r",stdin);
//    freopen("C:\\Users\\Zmy\\Desktop\\out.txt","w",stdout);
#endif
    while(~scanf("%s",x))
    {
        cntd=;
        len=strlen(x);
        kmp_pre();
//        for (int i=1;i<=len;i++) printf("%3d ",i);PU
//        for (int i=1;i<=len;i++) printf("%3d ",Next[i]);PU
        ;)
        {
            d[cntd++]=Next[i];
            i=Next[i];
        }
        ;i>=;i--)
        {
            printf("%d ",d[i]);
        }
        printf("%d\n",len);
    }
    ;
}

POJ-2752 Seek the Name, Seek the Fame(KMP,前缀与后缀相等)的更多相关文章

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

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

  2. (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 ...

  3. 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的简单应 ...

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

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

  5. 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 ...

  6. 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 ...

  7. 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 ...

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

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

  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 ...

  10. 题解报告:poj 2752 Seek the Name, Seek the Fame(kmp前缀表prefix_table的运用)

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

随机推荐

  1. hdu3639 强连通

    题意:有 n 个人,m 组支持关系,已知支持关系可以传递,比如 A 支持 B,则所有支持 A 的人也同时支持 B,问哪些人获得的支持数最多,最多获得多少支持(自己不能获得自己的支持). 首先,如果一些 ...

  2. IntelliJ IDEA 开发前的设置

    1.IntelliJ IDEA 显示行号方法 设置方法:File->Settings->Editor->General->Appearance->Show line nu ...

  3. PHP递归题目

    $arr = [ 'a' => 'A', 'b' => 'B', 'c' => [ 'd'=> 'D', 'e'=>[ 'f'=>'F', 'g'=>['h' ...

  4. 锁_rac环境kill锁表会话后出现killed状态(解决)

    原创作品,出自 "深蓝的blog" 博客,深蓝的blog:http://blog.csdn.net/huangyanlong/article/details/46876961 ra ...

  5. lucene文件格式待整理

    这是之前Lucene3.0生成的索引格式 a表

  6. 【HACK】破解APK并注入自己的代码

    请不要去干坏事! 使用工具: APKTool 提醒:能够正常安装到手机上的APK都是带有签名的(不了解签名的可以百度),APK在破解重新打包后是已经不再拥有签名的,如果想要你破解后的APK能够正常运行 ...

  7. unity3d用鼠标拖动物体的一段代码

    1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 这是一段拖动物体的代码,比较简洁明了,对了解uni ...

  8. Maximum number of WAL files in the pg_xlog directory (1)

      Guillaume Lelarge: Hi, As part of our monitoring work for our customers, we stumbled upon an issue ...

  9. Linux进程间通信-信号

    1.什么是信号信号是Linux系统响应某些条件而产生的一个事件,接收到该信号的进程会执行相应的操作. 2.信号的产生1)由硬件产生,如从键盘输入Ctrl+C可以终止当前进程2)由其他进程发送,如可在s ...

  10. Entity Framework只entity与DbContext的分离

    说明:以下例子采用的是DB first的模式 在之前的webform开发模式中我们习惯性性的会建立这样的一些类库:Model.DAL.BLL...但是在用了EF以后,我们创建的ADO.NET实体数据模 ...