HDU 4821 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 的子串,比如样例中abcabcbcaabc应该分成abcabcbcabcabcbcaacabcbcaababcbcaabc。再将这些子串分成 m 个长度为 l 的小子串,比如abcabcbca应该分成abcabcbca;如果某个子串中所有的小子串没有重复,则总值加一,最后输出这个总值。
题解: 就是题目的理解上比较费劲,但本质上就是一道简单的字符串哈希的题目。值得注意的是,当字符串总长度足够长的时候,第 i 个子串中包含的小子串,和第 i+l 个子串中包含的小子串是有重合的,这个大家可以自己思考验证一下。所以可以从这个切入点去优化一下,减小时间复杂度。
因此我们希望在检查了第 i 个子串中小子串是否有重复之后,不要第一时间去检查第 i+1个子串,而应该是第 i+l 个子串。这样我们只需要将之前所记录,删除第一个小子串之后,在记录下一个小子串即可判重。实现方法是借助map,虽然set自带去重功能,但是你不知道这个小子串到底出现了几次,因此在删除操作上比较麻烦。

#include<iostream>
#include<algorithm>
#include<cstring>
#include<map>
using namespace std;
typedef unsigned long long ull;

int m, l;
ull tem[100005] = {1};
char s[100005];
ull hs[100005];
int main(){
    for (int i = 1; i < 100005; i++)    tem[i] = tem[i - 1] * 131;
    while(~scanf("%d%d", &m, &l)){
        scanf("%s", s + 1);
        int sum = 0;
        int len = strlen(s + 1);
        for (int i = 1; i < 100005; i++)    hs[i] = 0;
        for (int i = 1; i <= len; i++)      hs[i] = hs[i - 1] * 131 + s[i] - 'a';
        for (int i = 0; i < l; i++){
            int flag = 0;
            map<ull, int> mp;
            for (int j = 1, k = 1; i + j * l <= len; j++){
                ull a = hs[i + j * l] - hs[i + (j - 1) * l] * tem[l];
                if(mp[a]==1)        flag++;
                mp[a]++;
                if(j >= m){
                    if(!flag)       sum++;
                    ull b = hs[i + k * l] - hs[i + (k - 1) * l] * tem[l];
                    k++, mp[b]--;
                    if(mp[b]==1)    flag--;
                }
            }
        }
        printf("%d\n", sum);
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值