SCU4438——经典哈希+前缀和

题目链接:http://acm.scu.edu.cn/soj/problem.action?id=4438

 

 

Censor

frog is now a editor to censor so-called sensitive words (敏感词).

She has a long text pp. Her job is relatively simple -- just to find the first occurence of sensitive word ww and remove it.

frog repeats over and over again. Help her do the tedious work.

Input

The input consists of multiple tests. For each test:

The first line contains 11 string ww. The second line contains 11 string pp.

(1≤length of w,p≤5⋅1061≤length of w,p≤5⋅106, w,pw,p consists of only lowercase letter)

Output

For each test, write 11 string which denotes the censored text.

Sample Input

    abc
    aaabcbc
    b
    bbb
    abc
    ab

Sample Output

    a
    
    ab

 

题目翻译:

给一个串s和t,求t中删除s后的串。

哈希处理字符串,之后枚举就可以了。

#include <iostream>
#include<cstring>
#define ull unsigned long long
using namespace std;
const int maxn=1e7+7;
const ull P=999983; 
ull Hash[maxn],p[maxn];
char s1[maxn],s2[maxn];
char tt[maxn];
int main(int argc, char** argv) {
	p[0]=1;
	for(int i=1;i<=maxn;++i)
		p[i]=p[i-1]*P;
	while(~scanf("%s%s",s1,s2)){
		int len1=strlen(s1);
		int len2=strlen(s2);
		if(len1>len2){
			printf("%s\n",s2);
			continue;
		} 
		ull temp=0,cnt=0;
		for(int i=0;i<len1;++i)
			temp=temp*P+s1[i]-'a'+1;
		Hash[0]=0;
		for(int i=0;i<len2;++i){
			tt[cnt++]=s2[i];
			Hash[cnt]=Hash[cnt-1]*P+s2[i]-'a'+1;
			if(cnt>=len1&&Hash[cnt]-Hash[cnt-len1]*p[len1]==temp)
				cnt-=len1;
		}
		for(int i=0;i<cnt;++i) printf("%c",tt[i]);
		printf("\n");
	}
	return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值