• 字符串hash


    #include<iostream>
    #include<iomanip>
    #include<cstdio>
    #include<vector>
    #include<cmath>
    #include<string>
    #include<cstring>
    #include<algorithm>
    #include<unordered_map>
    #include<unordered_set>
    #include<queue>
    #include<map>
    #include<set>
    #define  endl '
    '
    #define all(s) s.begin(),s.end()
    #define lowbit(x) (x&-x)
    #define rep(i,a,n) for (int i=a;i<=n;i++)
    #define IOS ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    #define mem(a,b) memset(a,b,sizeof(a))
    #define fi first
    #define se second
    #define pb push_back
    #define pi acos(-1.0)
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    typedef pair<int,int> pii;
    const int mod = 1e9 + 7;
    const double eps = 1e-8;
    const int inf = 0x3f3f3f3f;
    const ll INF = 0x3f3f3f3f3f3f3f3f;
    const int N = 2e5 + 10;
    const int maxn = 2e5 + 10;
    struct ha{
        ull h[maxn],p[maxn], P = 131;
        void k_hash(string s)    {
            p[0]=1;
            h[0]=s[0];
            for(int i=1;i<s.size();i++){
                p[i]=p[i-1]*P;    //次方
                h[i]=h[i-1]*P+s[i];    //hash值
            }
        }
        ull get_hash(int left,int right){
            if(left > right) return 0;
            return left?h[right]-h[left-1]*p[right-left+1]:h[right];
        }
    }hash1 , hash2;
    int main()
    {
        string s, t;
        cin >> s >> t;
        hash1.k_hash(s);
        hash2.k_hash(t);
        ll ans = 0;
        for(int i = 0; i < s.length(); i++){
            if(i >= t.length() - 1) break;
            if(hash1.get_hash(0, i) == hash2.get_hash(0, i)){
                int l = 0, r = min(t.length() - i - 1, s.length() - 1);
                while(l <= r){
                    int mid = l + r >> 1;
                    if(hash1.get_hash(0, mid) == hash2.get_hash(i + 1, i + 1 + mid)) l = mid + 1;
                    else r = mid - 1;
                }
                ans += r + 1;
            }
        }
        cout << ans;
    }
    rush!
  • 相关阅读:
    idea快捷键
    idea抛异常方式
    scott登陆PLSQL时候出现insufficient privileges的解决方法
    Linux下磁盘实战操作命令
    Docker容器常用命令
    Docker启动守护式容器
    Docker命令总结
    Docker镜像常用命令
    Centos7系统Docker安装
    Docker组成三要素
  • 原文地址:https://www.cnblogs.com/LH2000/p/14422861.html
Copyright © 2020-2023  润新知