2021 ccpc 威海 D. Period(字符串哈希 or next数组)

传送门

题意:给你一个字符串,q次查询,每次查询会将字符串中的一个字符修改为#,求在新串中可以选出几种长度不同的前后缀,使得前后缀相同

分析:对于长度为n的串,#在pos位置时,只需对长度小于等于min(pos-1,n-pos)的前后缀查询即可 
q在1e6 故要先预处理主串看看有哪些长度的前后缀是相同的 再O(1)查询 

字符串哈希做法

#include<bits/stdc++.h>
using namespace std;
#define ull unsigned long long
const int maxn=1e6+5;
const int p=131;
ull hs[maxn];//字符串哈希前缀和 
ull bin[maxn];//进制 
char c[maxn];
int ans[maxn];
int n;
 
void gethash()//预处理主串 
{
    bin[0]=1;
    for(int i=1;i<=n;i++)
    {
        hs[i]=hs[i-1]*p+c[i];
        bin[i]=bin[i-1]*p;
    }
}
 
ull getsub(int l,int r)//子串哈希值 
{
    return hs[r]-hs[l-1]*bin[r-l+1];
}
 
int main()
{
    scanf("%s",c+1);
    n=strlen(c+1);
    gethash();
    for(int i=1;i<=n/2;i++)
    {
        if(getsub(1,i)==getsub(n-i+1,n))
        {
            ans[i]=ans[i-1]+1;//前缀长度为i时能取到的最大方案数 
        }
        else ans[i]=ans[i-1];
    }
    int m;
    cin>>m;
    while(m--)
    {
        int pos;
        scanf("%d",&pos);//1e6
        pos=min(pos-1,n-pos); 
        printf("%d\n",ans[pos]);
    }
}

next数组做法

#include<bits/stdc++.h>
using namespace std;
const int maxn=1e6+5; 
char s[maxn];
int n;
int nxt[maxn];
int ans[maxn];

void getnext()
{
    int j=0;
    for(int i=2;i<=n;i++)
    {
        while(j&&s[i]!=s[j+1]) j=nxt[j];
        if(s[i]==s[j+1]) j++;
        nxt[i]=j;
    }
}

int main()
{
    scanf("%s",s+1);
    n=strlen(s+1);
    getnext();
    
    int p=nxt[n];
    while(p) ans[p]++,p=nxt[p];
    for(int i=1;i<=n/2;i++) ans[i]+=ans[i-1];

    int m;
    cin>>m;
    while(m--)
    {
        int pos;
        scanf("%d",&pos);
        pos=min(pos-1,n-pos); 
        printf("%d\n",ans[pos]);
    }
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

chmpy

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值