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;
    }
    


    东北日出西边雨 道是无情却有情
  • 相关阅读:
    编程模式
    第六章类(十九)readonly
    Javascript----实现鼠标背景效果(同时不影响其操作)
    Javascript----input事件实现动态监听textarea内容变化
    javascript----mouseover和mouseenter的区别
    Javascript----实现火箭按钮网页置顶
    Javascript----scroll事件进度条监听
    Javascript----生成省-市下拉表单
    Javascript----增删改查
    javascript-----轮播图插件
  • 原文地址:https://www.cnblogs.com/ccut-ry/p/9380281.html
Copyright © 2011-2022 走看看