zoukankan      html  css  js  c++  java
  • poj2752Seek the Name, Seek the Fame(next数组)

    Seek the Name, Seek the Fame
    Time Limit: 2000MS   Memory Limit: 65536K
    Total Submissions: 24390   Accepted: 12723

    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 to their newly-born babies. They seek the name, and at the same time seek the fame. In order to escape from such boring job, the innovative little cat works out an easy but fantastic algorithm:

    Step1. Connect the father's name and the mother's name, to a new string S.
    Step2. Find a proper prefix-suffix string of S (which is not only the prefix, but also the suffix of S).

    Example: Father='ala', Mother='la', we have S = 'ala'+'la' = 'alala'. Potential prefix-suffix strings of S are {'a', 'ala', 'alala'}. Given the string S, could you help the little cat to write a program to calculate the length of possible prefix-suffix strings of S? (He might thank you by giving your baby a name:)

    Input

    The input contains a number of test cases. Each test case occupies a single line that contains the string S described above.

    Restrictions: Only lowercase letters may appear in the input. 1 <= Length of S <= 400000.

    Output

    For each test case, output a single line with integer numbers in increasing order, denoting the possible length of the new baby's name.

    Sample Input

    ababcababababcabab
    aaaaa
    

    Sample Output

    2 4 9 18
    1 2 3 4 5
    

    Source

    题意:求一个串中满足前缀等于后缀的子串的长度
    这里要跟回文串区别开来,回文是从前往后和从后往前完全匹配;
    而这个是前面一部分与后面一部分完全匹配。
    题解:kmp中求出的next数组,本身就是具有这个结构,不过next数组中
    求出的是当前的最大前缀等于后缀的前缀子串的位置。
    那么只要递归输出这个next数组就行了。
    代码:
    #include<iostream>
    #include<string.h>
    #include<string>
    #include<stdlib.h>
    #include<algorithm>
    #include<stdio.h>
    #include<queue>
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    typedef long long ll;
    typedef pair<int,int> PII;
    #define mod 1000000007
    #define pb push_back
    #define mp make_pair
    #define all(x) (x).begin(),(x).end()
    #define fi first
    #define se second
    //head
    #define MAX 400005
    int next[MAX];
    char s[MAX];
    int len;
    void get_next()
    {
        int i=0,j=-1;
        next[i]=j;
        for(i=1;i<len;i++)
        {
            while(j>-1&&s[j+1]!=s[i])
                j=next[j];
            if(s[j+1]==s[i]) j++;
            next[i]=j;
        }
    }
    void output(int x)
    {
        if(next[x]==-1)
        {
            printf("%d ",x+1);
            return ;
        }
        output(next[x]);
         printf("%d ",x+1);
        //cout<<"ok"<<endl;
    }
    int main()
    {
        /*ios_base::sync_with_stdio(0);
        cin.tie(0);
        cout.tie(0);*/
         while(~scanf("%s",s))
         {
             memset(next,0,sizeof(next));
             len=strlen(s);
             get_next();
             /*for(int i=0;i<len;i++)
                cout<<next[i]<<" ";
                cout<<endl;*/
             output(len-1);
             printf("
    ");
         }
        return 0;
    }
     
     
     
     
     
     
     
     
     
  • 相关阅读:
    jni中调用java方法获取当前apk的签名文件md5值
    Android底层驱动开发(一)
    作为原作者你能忍吗?
    SUN dataset图像数据集下载
    计算机视觉和图像处理常用的一些标准图片
    Docker基础技术:DeviceMapper
    Docker基础技术:AUFS
    Docker基础技术:Linux CGroup
    Docker基础技术:Linux Namespace(下)
    Docker基础技术:Linux Namespace(上)
  • 原文地址:https://www.cnblogs.com/zhgyki/p/9641793.html
Copyright © 2011-2022 走看看