HDU 4821 String (字符串哈希)

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

题目大意:
给定一个字符串s,问有多少个子串长度为M*L,该子串由连续M个长度为L的小子串构成,每个小子串不能相同

思路:
字符串哈希应该不难想,判断长度为M*L的子串是否能够分成M个不相同的长度为L的小子串,判断哈希值是否相同即可。一开始直接暴力哈希,成功TLE。后来看了看别人的博客,才明白如何优化。

当我们判断完一个子串时,我们可以继续进行如下操作:

  1. 在该子串末尾处添加一个长度为L的小子串
  2. 将开头的一个长度为L的小子串删去
  3. 判断此时是否满足M个长度为L的不同小子串
  4. 一直到末尾结束

这样第一重循环只需要L次便能遍历所有的子串了

代码如下:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <queue>
#include <string>
#include <unordered_map>

using namespace std;
typedef long long  ll;
typedef unsigned long long ull;
typedef pair<ll, ll> PII;

const int N = 1e6 + 7, M = 1e5 + 6, P = 131;
const int mod = 10007;
const int inf = 0x3f3f3f3f;

char str[N];
ull p[N], h[N];
unordered_map<ull, int> mm;
//map自动去重yyds
ull get_hash(int l, int r)
{
    return h[r] - h[l - 1] * p[r - l + 1];
}
int main()
{
    ios::sync_with_stdio(false);
    int m, l;
    while (cin >> m >> l)
    {
        int ans = 0;
        cin >> str + 1;
        int len = strlen(str + 1);
        p[0] = 1;
        for (int i = 1; i <= len; i ++ )
        {
            p[i] = p[i - 1] * P;
            h[i] = h[i - 1] * P + str[i];
        }
        for (int i = 1; i <= l && i + m * l - 1 <= len; i ++ )
        {
            mm.clear();
            for (int j = i; j < i + m * l; j += l)
                mm[get_hash(j, j + l - 1)] ++ ;

            if (mm.size() == m) ans ++;
            //删头去尾,需要细细品味
            for (int j = i + m * l; j + l - 1 <= len; j += l)
            {
                ull x = get_hash(j, j + l - 1);
                mm[x] ++;
                x = get_hash(j - m * l, j - m * l + l - 1);
                mm[x] --;
                if (mm[x] == 0) mm.erase(x);
                if (mm.size() == m) ans ++;
            }
        }
        cout << ans << "\n";
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值