您的位置:首页 > 其它

cf#342-B - War of the Corporations

2016-02-15 00:33 232 查看
http://codeforces.com/contest/625/problem/B

题意:给出s子串

和t子串

求 s子串要替换多少个字符为#才能使得 在s里面完全找不到t

直接kmp找出所有的 不重复子串个数即可

裸的kmp啦。。

#include <cstdio>
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <iostream>
using namespace std;
#define lson l , m , rt << 1
#define rson m + 1 , r , rt << 1 | 1
const __int64 inf=2147483647;
const double pi=acos(-1.0);
double eps=0.000001;
__int64 min(__int64 a,__int64 b)
{return a<b?a:b;}
__int64 max(__int64 a,__int64 b)
{
	return a<b?b:a;
}
const int maxn = 100005; 

int nextval[maxn];
void get_next(char *t,int len)	//失配函数

{
	int i,j;
	i=1; 
	nextval[1]=0;
	j=0;
	while(i<len)
	{
		
		if (j==0||t[i]==t[j])
		{ 
			j++;
			i++;  
			if (t[i]!=t[j])
				nextval[i]=j;	
			else
				nextval[i]=nextval[j];
			
			
		}
		else
			j=nextval[j];
	}
	
}

int kmp(char* s,char* t,int len_s,int len_t)
{
	int i=1;
	int j=1;
	while(i<=len_s&&j<=len_t)
	{
		if (j==0||s[i]==t[j])
		{
			i++;j++;
		}
		else
			j=nextval[j];
	}
	if (j>len_t)
		return i-len_t;
	else
		return 0;
	
}

char tm[maxn];
char nm[500];

int main( )
{
 
		
		scanf("%s",tm+1);  
		scanf("%s",nm+1); 
int n=strlen(tm+1);
int m=strlen(nm+1);
		get_next(nm,m);
	int cun=0;
	int st=0;
		while(1)
		{
			int ret=	kmp(tm+st,nm,n,m);
			if (!ret) break;
			else
			{
				cun++;
				st+=ret+m-1;
			}
		}
		printf("%d\n",cun);
		
		 
	
	
	
	
	return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: