原题目:
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
abcaaabcbcbbbbabcab
Sample Output
aab
中文概要:
给出一个word字符串和一个page字符串,要求不断重复把p中所有w串删去.
输出最终结果(eg: w=abc p=aaabcbc => ans=a )
#include<queue>
#include<set>
#include<map>
#include<cstdlib>
#include<cstring>
#include<iostream>
using namespace std;
typedef unsigned long long ULL;//若哈希结果超出LL的范围可以自动取模LL_max
const int maxn = 5e6 + 50;
char w[maxn], p[maxn], ans[maxn];
struct Hash
{ULL hash=131;ULL hash_b[maxn];//字符串哈希表(用于在母串中p[i]-p[i-len]*has_b[len]时使用,这时得到的才是该子串的哈希值)ULL w_hash;//w串的哈希值 ULL w_len;//w串的长度ULL p_hash[maxn];//p串的哈希值 void init()//初始化{w_hash=0;//串哈希值初始化p_hash[0]=0;hash_b[0]=1;//哈希表初始化for(int i=1;i<(int)5e6;i++)hash_b[i]=hash_b[i-1]*hash;}bool check(int pos){if(pos >= w_len&&p_hash[pos] - p_hash[pos - w_len] * hash_b[w_len] == w_hash)//判断子串是否与母串pos开始len长度串匹配成功return true;return false;}void solve(){w_len=strlen(w);for(int i=0;i<w_len;i++)//获得w串的哈希值w_hash=w_hash*hash+(w[i]-'a'+1);int top=0,p_len=strlen(p);//top为ans串的长度for(int i=0;i<p_len;i++)//ans为输出的串,ans从1开始赋值,有与w串相同的就直接删除 {ans[top++]=p[i];p_hash[top]=p_hash[top-1]*hash+(p[i]-'a'+1);if(check(top))top-=w_len;//边计算哈希值边删 }for(int i=0;i<top;i++)printf("%c",ans[i]);printf("\n");}
}H;
int main()
{while(~scanf("%s%s",&w,&p)){H.init();H.solve();}return 0;
}
思路:
设一个ans串,在用p串不断给ans串赋值的时候同时来判断是否有与w串相同的,有就直接删除
说实话是第一次看结构体套函数的例子,原来不知道结构体还能套函数,妈的,原来结构体这么吊
感觉本题难点在于判断子串是否与母串pos开始len长度串匹配成功
也就是check函数中if(pos >= w_len&&p_hash[pos] – p_hash[pos – w_len] * hash_b[w_len] == w_hash)