zoukankan      html  css  js  c++  java
  • poj 4468Spy(kmp算法)

    Spy

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 204    Accepted Submission(s): 96

    Problem Description
    “Be subtle! Be subtle! And use your spies for every kind of business. ”
    — Sun Tzu
    “A spy with insufficient ability really sucks”
    — An anonymous general who lost the war
    You, a general, following Sun Tzu’s instruction, make heavy use of spies and agents to gain information secretly in order to win the war (and return home to get married, what a flag you set up). However, the so-called “secret message” brought back by your spy, is in fact encrypted, forcing yourself into making deep study of message encryption employed by your enemy.
    Finally you found how your enemy encrypts message. The original message, namely s, consists of lowercase Latin alphabets. Then the following steps would be taken:
    * Step 1: Let r = s
    * Step 2: Remove r’s suffix (may be empty) whose length is less than length of s and append s to r. More precisely, firstly donate r[1...n], s[1...m], then an integer i is chosen, satisfying i ≤ n, n - i < m, and we make our new r = r[1...i] + s[1...m]. This step might be taken for several times or not be taken at all.
    What your spy brought back is the encrypted message r, you should solve for the minimal possible length of s (which is enough for your tactical actions).
     
    Input
    There are several test cases.
    For each test case there is a single line containing only one string r (The length of r does not exceed 10 5). You may assume that the input contains no more than 2 × 10 6 characters.
    Input is terminated by EOF.
     
    Output
    For each test case, output one line “Case X: Y” where X is the test case number (starting from 1) and Y is the desired answer.
     
    Sample Input
    abc aab abcadabcabcad aaabbbaaaabbbaa abcababcd
     
    Sample Output
    Case 1: 3 Case 2: 2 Case 3: 5 Case 4: 6 Case 5: 4
     
    Source
     

    第一次做这种题也是第一次接触kmp算法,感觉很糟糕,这个题有好多不懂的地方,看着解题报告写的,写完了提交78ms,看人家的好多31ms,想自己优化一下,改了半天还是78ms,可能还是因为没有真正理解它吧,不过以后还会继续看的,继续练习,总会学会的

    说一下题意,我觉得这个题目还挺难理解的,可能是英语太差了,连着看了好多解题报告才把题目搞懂,悲催。。。定义串B是由串A的若干前缀加在一起再加上串A本身构成的。现在给出串B,求串A的最小长度。我解释一下这个若干前缀的意思,比如A串为abcdef,a、ab、abc、abcd等都可以说是A的前缀,也就是从前往后的某一段字符串

    #include<stdio.h>
    #include<string.h>
    char s[100050],c[100050];
    int next[100050],n,cn;
    
    void match()
    {
    	int i,j=0,tem,k,a;
    	for(i=1;i<=n;i++)
    	{
    		while(j&&c[j+1]!=s[i])
    			j=next[j];
    		if(s[i]==c[j+1])
    			j++;
    		if(j==0)
    		{
    			for(k=tem;k<=i;k++)
    			{
    				c[++cn]=s[k];
    					a=cn;
    				if(c[a]==c[next[a-1]+1])
    					next[a]=next[a-1]+1;
    				}
    				tem=i+1;
    			}
    		}
    		else if(j==cn)
    			tem=i+1;//记录当前s位置的下一个位置
    	}
    	cn+=n-tem+1;
    }
    
    int main()
    {
    	int i,j,cas=1;
    	while(scanf("%s",s+1)!=EOF)
    	{
    		n=strlen(s+1);
    		memset(c,0,sizeof(c));
    		memset(next,0,sizeof(next));
    		c[1]=s[1];
    		cn=1;
    		match();
    		printf("Case %d: %d
    ",cas++,cn);
    	}
    	return 0;
    }


  • 相关阅读:
    好元素(good)
    三条线 (Standard IO)
    计数排序-自然顺序Comparable
    贪心算法之田忌赛马
    bzoj3400
    bzoj1704
    CF Round #456 (Div. 2)
    LA3029
    bzoj3000
    bzoj3623
  • 原文地址:https://www.cnblogs.com/xinyuyuanm/p/3212005.html
Copyright © 2011-2022 走看看