https://www.acwing.com/problem/content/162/
预处理+hash + 二分

#include <iostream>
#include <algorithm>
#include <string.h>

using namespace std;
typedef unsigned long long ull;
int a,b,q;
const int N=200000+10;
char s1[N],s2[N];

ull h1[N],h2[N],p[N];


ull geth1(int l,int r){
    return h1[r]-h1[l-1]*p[r-l+1];
}

ull geth2(int l,int r){
    return h2[r]-h2[l-1]*p[r-l+1];
}

int st[N];
int main()
{
    cin>>a>>b>>q;
    cin>>s1+1>>s2+1;
    //hash s1,s2;
    p[0]=1;
    for(int i=1;i<=a;i++) p[i]=p[i-1]*131;
    for(int i=1;i<=a;i++) h1[i]=h1[i-1]*131+s1[i]-'a'+1;
    for(int i=1;i<=b;i++) h2[i]=h2[i-1]*131+s2[i]-'a'+1;

    //预处理 + 二分
    for(int i=1;i<=a;i++){
        if(s1[i]!=s2[1]) {
            st[0]++;
            continue;
        }
        int l=1,r=min(b,a-i+1);
        while(r>l){
            int mid=l+r+1>>1;
            if(geth1(i,i+mid-1)==geth2(1,mid)) l=mid;
            else r=mid-1;
        }
        st[r]++;
    }
    while(q--){
        int num;
        cin>>num;
        cout<<st[num]<<endl;
    }
    return 0;
}

 posted on 2019-08-09 14:45  谁是凶手1703  阅读(54)  评论(0)    收藏  举报