zoukankan      html  css  js  c++  java
  • 字符串 hash

    Censor

    frog is now a editor to censor so-called sensitive words (敏感词).

    She has a long text p

    . Her job is relatively simple -- just to find the first occurence of sensitive word w

    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 1

    string w. The second line contains 1 string p

    .

    (1length of w,p5106

    , w,p

    consists of only lowercase letter)

    Output

    For each test, write 1

    string which denotes the censored text.

    Sample Input

        abc
        aaabcbc
        b
        bbb
        abc
        ab

    Sample Output

        a
        
        ab

    题意: 给你两个字符串,要求每次可执行的操作是再下面的串里删去第一个串,直到不能删为止
    思路分析 : 先对模式串进行一次 hash ,然后再对主串进行依次 hash ,然后依次检索每个位置,当二者的 hash 值相同时即代表是相同的字符串,在这里我们只是 hash 了一次,并且默认了不同的字符串hash值一定是不同的
    代码示例:
    using namespace std;
    #define ll unsigned long long
    const ll maxn = 5e6+5;
    
    char a[maxn], b[maxn];
    ll len1, len2;
    ll hash_num;
    ll p = 19873;
    ll pp[maxn];
    
    void init(){
        pp[0] = 1;
        for(ll i = 1; i <= 5000000; i++){
            pp[i] = pp[i-1]*p;        
        }
    }
    
    void cal(){
        hash_num = 0;
        
        for(ll i = 1; i <= len1; i++){
            hash_num = hash_num*p+(a[i]-'a');
        }
    }
    
    ll sta[maxn];
    ll ha[maxn];
    
    bool check(ll pos){
        
        if (pos >= len1 && ha[pos]-ha[pos-len1]*pp[len1] == hash_num){
            return true;
        }
        return false;
    }
    
    int main() {
        //freopen("in.txt", "r", stdin);
        //freopen("out.txt", "w", stdout);
        init();
        
        while(~scanf("%s%s", a+1, b+1)){
            len1 = strlen(a+1);
            len2 = strlen(b+1);
            
            cal();        
            //prllf("+++  %llu 
    ", hash);    
            
            ll top = 1;
            ha[0] = 0;
            for(ll i = 1; i <= len2; i++){
                sta[top] = b[i];
                ha[top] = ha[top-1]*p+(b[i]-'a');
                if (check(top)) top -= len1;
                top++;    
            }
            for(ll i = 1; i < top; i++){
                printf("%c", sta[i]);
            }
            printf("
    ");
        }
        return 0;
    }
    


    东北日出西边雨 道是无情却有情
  • 相关阅读:
    2021,6,10 xjzx 模拟考试
    平衡树(二)——Treap
    AtCoder Beginner Contest 204 A-E简要题解
    POJ 2311 Cutting Game 题解
    Codeforces 990G GCD Counting 题解
    NOI2021 SDPTT D2T1 我已经完全理解了 DFS 序线段树 题解
    第三届山东省青少年创意编程与智能设计大赛总结
    Luogu P6042 「ACOI2020」学园祭 题解
    联合省选2021 游记
    Codeforces 1498E Two Houses 题解 —— 如何用结论吊打标算
  • 原文地址:https://www.cnblogs.com/ccut-ry/p/9380281.html
Copyright © 2011-2022 走看看