题意:
    给出一个字符串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. OnItemSelectedListener事件与二级联动

    一.界面 1.新建province.xml件. 在“res/values”位置新建province.xml文件. (1)province.xml文件位置如下图所示: (2)province.xml内容 ...

  2. Python字符串操作

    isalnum()判断是否都是有效字符串 >>> ev1 = 'evilxr' >>> ev2 = 'ev1il2xr3' >>> ev3 = ' ...

  3. 学习ARM7、ARM9的操作系统选择经验! [转]

    一 首先说说ARM的发展        可以用一片大好来形容,翻开各个公司的网站,招聘里面嵌入式占据了大半工程师职位.广义的嵌入式无非几种:传统的什么51.AVR.PIC称做嵌入式微控制器:ARM是嵌 ...

  4. 黑马程序员——JAVA基础之泛型和通配符

    ------- android培训.java培训.期待与您交流! ---------- 泛型:            JDK1.5版本以后出现新特性.用于解决安全问题,是一个类型安全机制. 泛型好处: ...

  5. (转) Deep Reinforcement Learning: Pong from Pixels

    Andrej Karpathy blog About Hacker's guide to Neural Networks Deep Reinforcement Learning: Pong from ...

  6. C# 大于屏幕的窗体

    1.使用SetWindowPos就可以做到这一点,只是最后一个参数要选对. RECT windowRect = new RECT(); User32.GetWindowRect(MyForm2.Han ...

  7. jQuery.fn.extend与jQuery.extend

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

  8. Oracle数据库—— PL/SQL进阶编程

    一.涉及内容 1.掌握PL/SQL程序块的结构 2.理解并熟练掌握各种变量的应用. 二.具体操作 1.创建一个表messages,该表只有一个字段results 类型是number(2),编写一个块, ...

  9. C数据类型

    结构体 因为数组中各元素的类型和长度都必须一致,以便于编译系统处理.为了解决这个问题,C语言中给出了另一种构造数据类型——“结构(structure)”或叫“结构体”.它相当于其它高级语言中的记录.“ ...

  10. linux服务之smtp

    实现这个协议的软件太多,有sendmail,postfix等.不像snmp,基本上是net-snmp一统天下, yum install nc nc用来取代telnet 这里我们希望让大家知道网络协议中 ...