cf#342-B - War of the Corporations

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;
} 


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值