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 
#include
#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&&Hash[cnt]-Hash[cnt-len1]*p[len1]==temp)
				cnt-=len1;
		}
		for(int i=0;i

 

你可能感兴趣的:(字符串哈希)