1. 程式人生 > >四川第七屆 C Censor (字符串哈希)

四川第七屆 C Censor (字符串哈希)

ren long long ack stack www pop multipl red lin

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 11string pp.

(1length of w,p5?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


題目大意;就是每組測試數據輸入兩個字符串;問第二個字符串中把第一個字符串一樣的刪除,問最後面還剩下什麽;並輸出;註意字符串刪除後還會形成一個新的字符串

#include<cstdio>
#include<string.h>
#include<vector>
#include<queue>
#include<stack>
#include<cmath>
#include<iostream>
#include<algorithm>
#include<deque>
using
namespace std; #define ll unsigned long long//要用長整型 char a[5000005]; char b[5000005]; ll p[5000005]; ll a131[5000005]; deque<ll>s; void init() {//先打表以防超時 a131[0]=1; for(ll i=1;i<=5000000;i++) { a131[i]=a131[i-1]*131; } } int main() { init(); while(~scanf("%s",&a)) { while(!s.empty()) s.pop_back(); memset(p,0,sizeof(p)); scanf("%s",&b); ll la=strlen(a); ll lb=strlen(b); ll ss=0; for(ll i=0;i<la;i++) {//對ss進行哈希 ss=(ss*131+a[i]); } ll cnt=1; for(ll i=0;i<lb;i++) { s.push_back(b[i]); if(s.size()==1) { p[cnt++]=b[i]; } else { p[cnt]=(p[cnt-1]*131+b[i]); cnt++; } if(s.size()>=la&&(p[cnt-1]-(p[cnt-la-1]*a131[la]))==ss)//選擇la長度的哈希,要減去之前的 {//一旦發現哈希相同,刪除相同字符串 for(ll j=1;j<=la;j++) { cnt--; s.pop_back(); } } } while(!s.empty()) { printf("%c",s.front()); s.pop_front(); } printf("\n"); } return 0; }



四川第七屆 C Censor (字符串哈希)