HDU - 4821 String(哈希+滑动窗口)

传送门


一开始以为怎么写都会超时,因为要以每个点为起点向后查找 M ∗ L M*L ML长度的字符串,以每个字符串为起点长度 L L L的子串的哈希可以预处理,但是如果向后滑动的话,所有长度为 L L L的哈希值都会改变,需要重新算吗?

答案是否定的,实际上我们只需考虑长度 [ 1 , L ] [1,L] [1,L]的字符作为起点,然后每次向后滑动 L L L的长度即可

#include <bits/stdc++.h>
#include <unordered_map>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define ins insert
#define Vector Point
#define lowbit(x) (x&(-x))
#define mkp(x,y) make_pair(x,y)
#define mem(a,x) memset(a,x,sizeof a);
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef pair<double,double> pdd;
const double eps=1e-8;
const double pi=acos(-1.0);
const int inf=0x3f3f3f3f;
const double dinf=1e300;
const ll INF=1e18;
const int Mod=1e9+7;
const int maxn=1e5+10;

const ull base=131;
ull bn[maxn],L[maxn],hashe[maxn];
unordered_map<ull,int> mp;
char s[maxn];
int m,l;

int main(){
    //freopen("in.txt","r",stdin);
    //freopen("out.txt","w",stdout);
    //ios_base::sync_with_stdio(0),cin.tie(0),cout.tie(0);
    hashe[0]=0,bn[0]=1;
    for(int i=1;i<maxn;i++) bn[i]=bn[i-1]*base;
    while(~scanf("%d%d",&m,&l)){
        scanf("%s",s+1);
        int n=strlen(s+1);
        if(m*l==1){  //特判长度为1
            printf("%d\n",n);
            continue;
        }
        for(int i=1;i<=n;i++) hashe[i]=hashe[i-1]*base+(ull)s[i];
        for(int i=1;i<=n-l+1;i++)  //预处理每个起点后长度为L的哈希值
            L[i]=hashe[i+l-1]-hashe[i-1]*bn[l];
        int ans=0;
        for(int k=1;k<=l && k+m*l<=n;k++){
            mp.clear();
            for(int i=k,j=1;j<=m;i+=l,j++){  //预处理开头的M*L
                mp[L[i]]++;
                //cout<<i<<" ";
            }
            //cout<<endl;
            if(mp.size()==m) ans++;
            for(int i=m*l+k;i<=n;i+=l){ 
                if(i>n-l+1) break;  //特判后面长度小于L的情况
                //cout<<i-m*l<<" "<<i<<endl;
                mp[L[i-m*l]]--,mp[L[i]]++;
                if(mp[L[i-m*l]]==0) mp.erase(L[i-m*l]);  //注意这里一定要删除
                if(mp.size()==m) ans++;
            }
            //cout<<"--------------"<<endl;
        }
        printf("%d\n",ans);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
字符串哈希滑动窗口是一种用于处理字符串的算法。它主要用于在给定的字符串中找到满足特定条件的子串。 在字符串哈希滑动窗口算法中,我们首先计算原始字符串的哈希值。然后,我们使用一个滑动窗口来遍历字符串,每次滑动一个固定长度的窗口。我们可以通过比较每个窗口内的子串的哈希值来判断是否满足条件。 具体而言,我们可以使用BKDRHash等哈希函数来计算字符串的哈希值。然后,我们枚举每个可能的起点,并使用滑动窗口来计算窗口内的子串的哈希值。通过比较窗口内的子串的哈希值,我们可以判断是否满足条件。 对于滑动窗口的移动,如果窗口内的子串满足条件,我们可以继续将窗口往右移动一个固定的长度。如果窗口内的子串不满足条件,我们将窗口的右边界移到最右端,并依次比较新窗口内的子串的哈希值。 综上所述,字符串哈希滑动窗口算法是通过计算字符串的哈希值,并使用滑动窗口来遍历字符串,以找到满足特定条件的子串。这个算法可以高效地处理字符串,并且能够应用于各种字符串相关的问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [String (字符串哈希+滑动窗口)](https://blog.csdn.net/weixin_43872264/article/details/107571742)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] - *2* *3* [【字符串hash+滑动窗口String HDU - 4821](https://blog.csdn.net/qq_45599865/article/details/111143633)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_1"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值