HDU 4821 String 字符串hash

本文介绍了一种通过枚举起点和使用哈希值快速求解子串的技术,旨在解决寻找特定长度且由多个不重复子串组成的字符串问题。该方法利用哈希函数优化了子串比较过程,采用两指针法在O(n)时间内找到所有符合条件的子串。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

String

 

Problem Description
 
Given a string S and two integers L and M, we consider a substring of S as “recoverable” if and only if
  (i) It is of length M*L;
  (ii) It can be constructed by concatenating M “diversified” substrings of S, where each of these substrings has length L; two strings are considered as “diversified” if they don’t have the same character for every position.

Two substrings of S are considered as “different” if they are cut from different part of S. For example, string "aa" has 3 different substrings "aa", "a" and "a".

Your task is to calculate the number of different “recoverable” substrings of S.
 

 

Input
 
The input contains multiple test cases, proceeding to the End of File.

The first line of each test case has two space-separated integers M and L.

The second ine of each test case has a string S, which consists of only lowercase letters.

The length of S is not larger than 10^5, and 1 ≤ M * L ≤ the length of S.
 

 

Output
For each test case, output the answer in a single line.
 

 

Sample Input
3 3 abcabcbcaabc
 

 

Sample Output
2
 

题意:

  给你M和L,和一个字符串S。

  要求找出S的子串中长度为L*M,并且可以分成M段,每段长L,并且M段都不相同的子串个数。

题解:

  枚举起点

  hash每个前缀串

  那么一段子串的hash值就可以快速求出

  twopointsO(n)求出长度M*L,,M个连续串是否相同

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<cmath>
#include<map>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
typedef unsigned long long ULL;
const long long INF = 1e18+1LL;
const double pi = acos(-1.0);
const int N = 5e5+10, MM = 1e3+20,inf = 2e9;

const LL mod = 10000019ULL;
inline LL read()
{
    LL x=0,f=1;char ch=getchar();
    while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return x*f;
}

map<ULL,int  > s;
ULL bhas[N],has[N],sqr[N];
int M,L;
char sa[N];
int vis[N];
int main() {
    sqr[0] = 1;
    for(int i = 1; i < N; ++i) sqr[i] = sqr[i-1] * mod;
    while(scanf("%d%d",&M,&L)!=EOF) {
        scanf("%s",sa+1);
        int n = strlen(sa+1);
        has[0] = 0;
        for(int i = 1; i <= n; ++i) {
            has[i] = has[i-1] * mod + sa[i] - 'a' + 1;
        }
        int ans = 0;
        for(int i = 1; i <= L && i + M * L - 1 <= n; ++i) {
            int cnt = 0;
            s.clear();
            int ll = 1,rr = 0;
            for(int j = i; j + L - 1 <= n; j += L) {
                int l = j, r = j + L - 1;
                ULL now = has[r] - has[l-1]*sqr[L];
                if(s[now] == 0){
                    bhas[++rr] = now;
                    if(rr - ll + 1 >= M)ans+=1;
                    s[now] = 1;
                    continue;
                }
                else {
                    while(ll <= rr && bhas[ll]!=now) {
                        s[bhas[ll++]] = 0;
                    }
                    s[bhas[ll++]] = 0;
                    bhas[++rr] = now;
                    if(rr - ll + 1 >= M)ans+=1;
                    s[now] = 1;
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/zxhl/p/7226544.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值