题解 CH1809 【匹配统计】


题目链接:Link

Problem

Solution

首先不难想到字符串Hash的做法,枚举A中的每个位置,二分匹配长度即可,时间复杂度\(O(n)\)
很明显正解应该要用KMP,但对于匹配出的一个f[i]我们只能知道“有一次长度至少为f[i]的匹配”,同时还可能会漏掉一些匹配位,怎么办?
基于值域的前缀和
即令cnt[x]=长度至少为x的匹配位的个数,易得答案等于cnt[x]-cnt[x+1]。
因此,我们可以先把由f数组可直接得出的匹配位统计进cnt数组。
对于哪些被遗忘的匹配位,由KMP的基本引理2可得分别是f[i]=nxt[i],nxt[nxt[i]],...。倒序循环累加一下即可。

Code

字符串Hash:

#include<cstdio>
#include<algorithm>
using namespace std;
typedef unsigned long long ULL;
const int maxn=200005;
const ULL P=13141;
char A[maxn],B[maxn];
ULL pm[maxn],ha[maxn],hb[maxn];
int N,M,Q,cnt[maxn];
inline int match(int p)
{
    int L=0,R=min(N-p+1,M),M,res=0;
    while(L<=R)
    {
        M=(L+R)>>1;
        if(ha[p+M-1]-ha[p-1]*pm[M]==hb[M]) L=M+1,res=M;
        else R=M-1;
    }
    return res;
}
int main()
{
#ifdef local
    freopen("pro.in","r",stdin);
#endif
    scanf("%d%d%d",&N,&M,&Q);
    scanf("%s%s",A+1,B+1);
//  printf("A=%s B=%s\n",A+1,B+1);
    pm[0]=1;
    for(int i=1;i<maxn;i++) pm[i]=pm[i-1]*P;
    for(int i=1;i<=N;i++) ha[i]=ha[i-1]*P+(A[i]-'a'+1);
    for(int i=1;i<=M;i++) hb[i]=hb[i-1]*P+(B[i]-'a'+1);
    for(int i=1;i<=N;i++)
    {
        cnt[match(i)]++;
//      printf("match(%d)=%d\n",i,match(i));
    }
    while(Q-->0)
    {
        int x; scanf("%d",&x);
        printf("%d\n",cnt[x]);
    }
    return 0;
}

KMP:

#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=200005;
char A[maxn],B[maxn];
int N,M,Q,cnt[maxn],nxt[maxn],f[maxn];
int main()
{
#ifdef local
    freopen("pro.in","r",stdin);
#endif
    scanf("%d%d%d",&N,&M,&Q);
    scanf("%s%s",A+1,B+1);
    nxt[1]=0;
    for(int i=2,j=0;i<=M;i++)
    {
        while(j&&B[i]!=B[j+1]) j=nxt[j];
        if(B[i]==B[j+1]) j++;
        nxt[i]=j;
    }
    for(int i=1,j=0;i<=N;i++)
    {
        while(j&&(j==M||A[i]!=B[j+1])) j=nxt[j];
        if(A[i]==B[j+1]) j++;
        f[i]=j;
    }
    for(int i=1;i<=N;i++) cnt[f[i]]++;
    for(int i=M;i>=1;i--) cnt[nxt[i]]+=cnt[i];
    while(Q-->0)
    {
        int x; scanf("%d",&x);
        printf("%d\n",cnt[x]-cnt[x+1]);
    }
    return 0;
}
优质内容筛选与推荐>>
1、testblog
2、03-数据类型
3、var、let与const三者之间的区别
4、javaScript从入门到精通2.md
5、python函数学习(一)


长按二维码向我转账

受苹果公司新规定影响,微信 iOS 版的赞赏功能被关闭,可通过二维码转账支持公众号。

    阅读
    好看
    已推荐到看一看
    你的朋友可以在“发现”-“看一看”看到你认为好看的文章。
    已取消,“好看”想法已同步删除
    已推荐到看一看 和朋友分享想法
    最多200字,当前共 发送

    已发送

    朋友将在看一看看到

    确定
    分享你的想法...
    取消

    分享想法到看一看

    确定
    最多200字,当前共

    发送中

    网络异常,请稍后重试

    微信扫一扫
    关注该公众号