zoukankan      html  css  js  c++  java
  • poj2752 kmp

    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
    题意:找字符串前后缀相同的数
    题解:kmp 的next数组应用(这个实在太高,真没想到)递归的求前缀,因为每次的Netx【slen-1】也是前缀的数,把前缀不断当成后缀处理就能推导下去
    参考的这位大牛的博客http://www.cnblogs.com/dongsheng/archive/2012/08/13/2636261.html
    #include<map>
    #include<set>
    #include<cmath>
    #include<queue>
    #include<stack>
    #include<vector>
    #include<cstdio>
    #include<iomanip>
    #include<cstdlib>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define pi acos(-1)
    #define ll long long
    #define mod 1000000007
    #define ls l,m,rt<<1
    #define rs m+1,r,rt<<1|1
    
    using namespace std;
    
    const double g=10.0,eps=1e-9;
    const int N=400000+5,maxn=1000000+5,inf=0x3f3f3f3f;
    
    int Next[N],slen;
    string str;
    
    void getnext()
    {
        int k=-1;
        Next[0]=-1;
        for(int i=1;i<slen;i++)
        {
            while(k>-1&&str[k+1]!=str[i])k=Next[k];
            if(str[k+1]==str[i])k++;
            Next[i]=k;
        }
    }
    void print(int x)
    {
        if(x==-1)return ;
        print(Next[x]);
        cout<<x+1<<" ";
    }
    int main()
    {
        ios::sync_with_stdio(false);
        cin.tie(0);
     //   cout<<setiosflags(ios::fixed)<<setprecision(2);
        while(cin>>str){
            slen=str.size();
            getnext();
            print(Next[slen-1]);
            cout<<slen<<endl;
        }
        return 0;
    }
    View Code
  • 相关阅读:
    54:代码审计-TP5框架审计写法分析及代码追踪
    53:代码审计-TP5框架及无框架变量覆盖反序列化
    52:代码审计-PHP项目类RCE及文件包含下载删除
    51:代码审计-PHP框架MVC类上传断点调试挖掘
    支配树学习笔记
    模拟费用流学习笔记
    python之元类、双下方法( 双下方法也叫魔术方法、 内置方法)
    java 注解
    java 反射
    java synchronized
  • 原文地址:https://www.cnblogs.com/acjiumeng/p/6812594.html
Copyright © 2011-2022 走看看