zoukankan      html  css  js  c++  java
  • 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
    ",maxl[1]);
         }
        return 0;
    }
    代码
  • 相关阅读:
    Linux下忘记MySQL密码的解决办法
    Jenkins——为什么使用持续集成?
    JBoss7部署EJB连接MySQL
    同一进程中的线程有哪些资源可以共享(转)
    基于ssh开发web项目-用户登录流程
    mysql-5.6.16安装流程
    Spring学习笔记
    Hibernate持久化对象状态、转换方法和操作步骤
    Hibernate配置文件与关联映射介绍
    Java的hashCode方法
  • 原文地址:https://www.cnblogs.com/zxhl/p/4772482.html
Copyright © 2011-2022 走看看