zoukankan      html  css  js  c++  java
  • 四川第七届 C Censor (字符串哈希)

    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,p51061≤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("
    ");
        }
        return 0;
    }


  • 相关阅读:
    [LeetCode] Sqrt(x)
    [LeetCode] Rotate Array
    【经典算法】贪心算法
    【经典算法——查找】二分查找
    ARP(Adress Resolution Protocol): 地址解析协议
    【经典算法】分治策略
    [LeetCode] Recover Binary Search Tree
    [LeetCode] Convert Sorted Array to Binary Search Tree
    python数据类型之字典(dict)和其常用方法
    简单了解hash
  • 原文地址:https://www.cnblogs.com/caiyishuai/p/8955069.html
Copyright © 2011-2022 走看看