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;
    }
     
     
     
     
     
     
     
     
     
  • 相关阅读:
    TCP/IP 三次握手四次挥手
    光端机2m是什么意思,光端机E1与2M有什么区别?
    电视光端机故障维护与指示灯说明
    电视光端机常见故障问题介绍
    传输设备,光端机的应用及故障分析
    RS485数据光端机产品特点及技术参数介绍
    网管交换机与非网管交换机的优缺点介绍
    网管型交换机相比普通交换机有哪些明显优势?
    什么叫POE交换机?POE交换机使用方法详解!
    交换机用光纤模块互连一端灯不亮或两端都不亮,如何处理?
  • 原文地址:https://www.cnblogs.com/zhgyki/p/9641793.html
Copyright © 2011-2022 走看看