题意:
    给出一个字符串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. HDU-4614 Vases and Flowers (线段树区间更新)

    题目大意:有n个花瓶,每个花瓶中只能放一朵花.两种操作,一种是从A开始放F朵花,如果有的花瓶中已经有花则跳过这个花瓶,往下一个花瓶放:第二种是将区间[A,B]之间花瓶中的花清空.如果是第一种操作,输出 ...

  2. Kali安装

    Kali Linux 前身是 BackTrack ,不过Kali Linux是基于Debian 的 Linux 发行版,而BackTrack 则是基于Uubntu的,现在BackTrack 已经不更新 ...

  3. java多线程:并发包中的信号量和计数栓的编程模型

    一:信号量的编程模型 package com.yeepay.sxf.test.atomic.test; import java.util.concurrent.Semaphore; /** * 测试信 ...

  4. Python如何规定对方输入的数字必须是整数?

    可以使用字符串str的isdigit方法判断字符串是否是一个仅有数字组成,也就是整数.如果是整数退出while循环,否则继续请求输入. 1 2 3 4 5 6 while True:     x =  ...

  5. (转) ICCV 2015:21篇最火爆研究论文

          ICCV 2015:21篇最火爆研究论文 ICCV 2015: Twenty one hottest research papers   “Geometry vs Recognition” ...

  6. Linux-某电商网站流量劫持案例分析与思考

    [前言] 自腾讯与京东建立了战略合作关系之后,笔者网上购物就首选京东了.某天在家里访问京东首页的时候突然吃惊地发现浏览器突然跳到了第三方网站再回到京东,心里第一个反应就是中木马了. 竟然有这样的事,一 ...

  7. Unity3D研究院编辑器之Editor的GUI的事件拦截

    OnGUI是Unity上一个时代的UI系统,而现在运行时的UI系统已经被UGUI取代,但是Editor的UI还是在用老的这一套GUI系统.比如unity编辑器里的所有窗口,布局,按钮,拖动条.滚动等等 ...

  8. jQuery.fn.extend与jQuery.extend

    jQuery.extend(),是扩展的jQuery这个类. 假设我们把jQuery这个类看成是人类,能吃饭能喝水能跑能跳,现在我们用jQuery.extend这个方法给这个类拓展一个能唱歌的技能.这 ...

  9. oracle参数与启停

    oracle随系统启动而启动 cs65-64桌面版orcle-11.2.0.4 启动监听器,后台进程,OEM. 注意: 如果只做一和三,只能启动后台进程,监听器不启动,如果只做二和三,只能启动监听器, ...

  10. android中的TextView控件

    我以前是搞ssh开发的,现在在搞android开发,所以简单学习了一下,对于自己所了解的做一个记录,也算是一个笔记吧,如果有什么不对的,希望大家给予一定的指导.  一.TextView的基本使用 Te ...