码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-04-26 23:38:38      阅读:406      评论:0      收藏:0      [点我收藏+]

标签:ssi   string   frog   front   pre   \n   define   main   repeat   

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 (字符串哈希)

标签:ssi   string   frog   front   pre   \n   define   main   repeat   

原文地址:https://www.cnblogs.com/caiyishuai/p/8955069.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!