zoukankan      html  css  js  c++  java
  • Seek the Name, Seek the Fame(Kmp)

    Seek the Name, Seek the Fame

    Time Limit : 4000/2000ms (Java/Other)   Memory Limit : 131072/65536K (Java/Other)
    Total Submission(s) : 12   Accepted Submission(s) : 7
    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 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
     题解:让在一个串中找字串,使这个字串即是串s的前缀又是后缀,从p【len】开始p数组代表失配的字符的位置, 那么这个失配的位置前面的串即是串s的后缀了;肯定这个串本来就是前缀,那么失配的位置就可以当作长度了;
    /***************************************************************************/
     
        如左图,假设黑色线来代表字符串str,其长度是len,红色线的长度代表next[len],根据next数组定义易得前缀的next[len]长度的子串和后缀next[len]长度的子串完全相同(也就是两条线所对应的位置)。我们再求出next[len]位置处的next值,也就是图中蓝线对应的长度。同样可以得到两个蓝线对应的子串肯定完全相同,又由于第二段蓝线属于左侧红线的后缀,所以又能得到它肯定也是整个字符串的后缀。

        所以对于这道题,求出len处的next值,并递归的向下求出所有的next值,得到的就是答案。

    所以每次只需要找此段匹配的长度就好

    /***************************************************************************/

    代码:用了个递归,浪了一下,竟然还没PE;kmp的原理,从len开始直接相当于了位置坐标加一,慢慢体味。。。

     1 #include<stdio.h>
     2 #include<string.h>
     3 const int MAXN=400010;
     4 char s[MAXN];
     5 int p[MAXN],len;
     6 void getp(){
     7     int i=0,j=-1;
     8     p[0]=-1;
     9     while(i<len){
    10         if(j==-1||s[i]==s[j]){
    11             i++;j++;
    12             p[i]=j;
    13         }
    14         else j=p[j];
    15     }
    16 }
    17 void print(int x){
    18     if(x<=0)return;
    19     print(p[x]);
    20     printf("%d ",x);
    21 }
    22 int main(){int i,j;
    23     while(~scanf("%s",s)){
    24         len=strlen(s);
    25         getp();
    26         /*for(i=0;i<=len;i++)printf("%d ",p[i]);puts("");
    27         j=len;
    28         while(j>=0){
    29             for(i=p[j];i<j;i++)printf("%c",s[i]);puts("");
    30             j=p[j];
    31         }*/
    32         print(len);puts("");
    33     }
    34     return 0;
    35 }
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<algorithm>
    using namespace  std;
    typedef long long LL;
    #define mem(x,y) memset(x,y,sizeof(x))
    #define SI(x) scanf("%d",&x)
    #define PI(x) printf("%d",x)
    #define P_ printf(" ")
    const int INF=0x3f3f3f3f;
    const int MAXN=400010;
    int p[MAXN];
    
    void getp(char* s){
    	int len=strlen(s);
    	int i=0,j=-1;
    	p[0]=-1;
    	while(i<len){
    		if(j==-1||s[i]==s[j]){
    			i++;j++;
    			p[i]=j;
    		}
    		else j=p[j];
    	}
    }
    /*
    void kmp(char *s,char* m,int& ans){
    	int len=strlen(m);
    	getp(s);
    	int len2=strlen(s);
    	int j=0,i=0;
    	while(i<len){
    		if(j==-1||s[j]==m[i]){
    			i++;j++;
    			if(j==len2){
    				ans++;
    			}
    		}
    		else j=p[j];
    	}
    }
    */
    int main(){
    	char s[MAXN];
    	int a[MAXN];
    	int tp;
    	while(~scanf("%s",s)){
    		getp(s);
    		tp=0;
    		int len=strlen(s);
    		while(len>0){
    			a[tp++]=len;
    			len=p[len];
    		}
    		sort(a,a+tp);
    	//	int k=unique(a,a+tp)-a;
    		for(int i=0;i<tp;i++){
    			if(i)P_;
    			PI(a[i]);
    		}puts("");
    	}
    	return 0;
    }
    

      

  • 相关阅读:
    CodeForces19D:Points(线段树+set(动态查找每个点右上方的点))
    CodeForces-816B:Karen and Coffee (简单线段树)
    CodeForces292D:Connected Components (不错的并查集)
    CodeForces546D:Soldier and Number Game(筛区间素数因子个数和)
    CoderForces343D:Water Tree(dfs序+线段树&&特殊处理)
    HihoCoder1706 : 末尾有最多0的乘积(还不错的DP)
    HihoCoder1705: 座位问题(STL)
    【CQ18阶梯赛第8场】题解
    阿里开源 Dragonwell JDK 重磅发布 GA 版本:生产环境可用
    5年时间,我从开发做到总裁的秘籍--如何提升技术型管理者的领导力
  • 原文地址:https://www.cnblogs.com/handsomecui/p/4711034.html
Copyright © 2011-2022 走看看