HDOJ 4468 Spy KMP


从第一个字符开始,如果s可以与t匹配就匹配,不能匹配就加上一段


Spy

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


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 6characters.
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
 


/* ***********************************************
Author        :CKboss
Created Time  :2015年09月02日 星期三 21时06分10秒
File Name     :HDOJ4468.cpp
************************************************ */

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <cmath>
#include <cstdlib>
#include <vector>
#include <queue>
#include <set>
#include <map>

using namespace std;

const int maxn=101000;

char s[maxn],t[maxn];
int f[maxn];
int n,m,last;

int main()
{
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);

	int cas=1;
	while(scanf("%s",s+1)!=EOF)
	{
		memset(f,0,sizeof(f));
		memset(t,0,sizeof(t));
		n=strlen(s+1); 
		last=0; m=1; t[m]=s[1];
		int j=0;
		for(int i=1;i<=n;i++)
		{
			while(j&&s[i]!=t[j+1]) j=f[j];
			if(s[i]==t[j+1]) j++;
			else
			{
				int p=f[m];
				for(int k=last+1;k<=i;k++)
				{
					t[++m]=s[k];
					while(p&&t[m]!=t[p+1]) p=f[p];
					if(t[m]==t[p+1]) f[m]=p+1;
					else f[m]=0;
				}
				j=m;
			}
			if(j==m) last=i;
		}
		printf("Case %d: %d\n",cas++,n+m-last);
	}
    
    return 0;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值