POJ 2752 Seek the Name, Seek the Fame kmp(后缀与前缀)

题意:

给你一个串T,找出串T的子串,该串既是T的前缀也是T的后缀。从小到大输出所有符合要求的串的长度。

分析:

首先要知道KMP的next[i]数组求得的数值就是串T中的[1,i-1]的后缀与串T中的[0,i-2]前缀的最大匹配长度。         所以next[m](m为串长且串从0到m-1下标)的值就是与后缀匹配的最大前缀长度(想想是不是)。

next[next[m]]也是一个与后缀匹配的前缀长度,,,依次类推即可。

题解来自饶齐:http://blog.csdn.net/u013480600/article/details/23024781

//作者:1085422276
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <ctime>
#include <iostream>
#include <algorithm>
#include <set>
#include <vector>
#include <sstream>
#include <queue>
#include <typeinfo>
//#include<bits/stdc++.h>
#include <map>
#include <stack>
typedef long long ll;
using namespace std;
const int inf = 10000000;
inline ll read()
{
    ll x=0,f=1;
    char ch=getchar();
    while(ch<‘0‘||ch>‘9‘)
    {
        if(ch==‘-‘)f=-1;
        ch=getchar();
    }
    while(ch>=‘0‘&&ch<=‘9‘)
    {
        x=x*10+ch-‘0‘;
        ch=getchar();
    }
    return x*f;
}
ll exgcd(ll a,ll b,ll &x,ll &y)
{
    ll temp,p;
    if(b==0)
    {
        x=1;
        y=0;
        return a;
    }
    p=exgcd(b,a%b,x,y);
    temp=x;
    x=y;
    y=temp-(a/b)*y;
    return p;
}
//*******************************
char a[400005];
    int maxl[400005],p[400005];
int main()
{

     while(gets(a)!=NULL)
     {
         int n=strlen(a);
          memset(p,0,sizeof(p));
          int j=0;
          for(int i=1;i<n;i++)
          {
              while(j>0&&a[j]!=a[i])j=p[j];
              if(a[j]==a[i])j++;
              p[i+1]=j;
          }
          int cnt=0;
          maxl[++cnt]=n;
          int i=n;
          while(p[n])
          {
              maxl[++cnt]=p[n];
              n=p[n];
          }
          for(int i=cnt;i>1;i--)
          {
             printf("%d ",maxl[i]);
          }
          printf("%d\n",maxl[1]);
     }
    return 0;
}

代码

时间: 2024-12-30 11:18:55

POJ 2752 Seek the Name, Seek the Fame kmp(后缀与前缀)的相关文章

poj 2752 求一个字符串所有的相同前后缀

求一个字符串所有的相同前后缀Sample Input ababcababababcababaaaaaSample Output 2 4 9 181 2 3 4 5 1 #include <iostream> 2 #include <cstring> 3 #include <cstdio> 4 #include <stack> 5 using namespace std; 6 7 const int N = 400010; 8 int next[N]; 9 c

poj 2752 Seek the Name, Seek the Fame KMP

对于KMP算法中next函数的应用 题意是对于一个字符串的前缀和后缀比较是否相等,再把相等的可能按字符串长度进行输出 #include <iostream> #include<stdio.h> #include<string.h> using namespace std; int len; int next[1000005]; char s[1000005]; int kmp_next() { int i=0,j=-1; next[0]=-1; while(i<l

(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: 65536K Total Submissions: 14611   Accepted: 7320 Description The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked

Seek the Name, Seek the Fame poj 2752

Seek the Name, Seek the Fame Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other) Problem Description The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the little cat to give n

POJ 2752 Seek the Name, Seek the Fame KMP题解

本题是KMP的next数组的灵活运用. 具体就是看最后整个数列的最后一个字母,能有多少前缀. 理解了next数组就很容易了. #include <stdio.h> #include <string.h> #include <vector> using std::vector; const int MAX_N = 400001; char name[MAX_N]; int next[MAX_N], len; void genNext() { for (int i = 1,

[kuangbin带你飞]专题十六 KMP &amp; 扩展KMP &amp; Manacher H - Seek the Name, Seek the Fame POJ - 2752(kmp的next数组应用)

H - Seek the Name, Seek the Fame POJ - 2752 题目链接:https://vjudge.net/contest/70325#problem/H 题目: The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the little cat to give names to their newly-born babies. Th

KMP中next的应用 POJ 2752 Seek the Name, Seek the Fame

Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 19163   Accepted: 9849 Description The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the little cat to give names t

POJ 2752 Seek the Name, Seek the Fame kmp失配函数next应用

点击打开链接 Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12791   Accepted: 6304 Description The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the little cat to give

POJ 2752 Seek the Name, Seek the Fame (KMP的next函数运用)

Seek the Name, Seek the Fame Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 14188   Accepted: 7068 Description The little cat is so famous, that many couples tramp over hill and dale to Byteland, and asked the little cat to give names t