HDU - 4821 String(hash+map去重)

String

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 4430    Accepted Submission(s): 1341


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

Source

2013 Asia Regional Changchun

题意:给出一个字符串S,输出S有多少个字串,这些子串由M个长度为L不相同的子串组成

思路:这道题目用到了hash,第一次用hash写题,以下来自http://www.cnblogs.com/zyf0163/p/4806951.html

------------------------------------------------------------------------------------------------------------------------------

首先我们会想一下二进制数。

对于任意一个二进制数,我们将它化为10进制的数的方法如下(以二进制数1101101为例):

hash用的也是一样的原理,为每一个前缀(也可以后缀,笔者习惯1 base,所以喜欢用前缀来计算,Hash[i] = Hash[i - 1] * x + s[i](其中1 < i <= n,Hash[0] = 0)。

 

一般地,

 

而对于l - r区间的hash值,则为:

 

但是如果n很大呢?那样不是会溢出了吗?

因此我们把hash值储存在unsigned long long里面, 那样溢出时,会自动取余2的64次方,but这样可能会使2个不同串的哈希值相同,但这样的概率极低(不排除你的运气不好)。

因此我们可以通过Hash值来比较两个字符串是否相等。

-----------------------------------------------------------------------------------------------------------------------------

一位一位的枚举复杂度为(len-ML)*(ML),肯定会超时,是用到了类似于滑动窗口的原理降低了复杂度,每次枚举L位,枚举了M个之后检查时候符合题意,然后去掉开头的L位,再在末尾加上L位,判断是否合法,最多只需要从L个位置开始滑动就行了,因为从L+1位置开始滑的之前在1位置开始滑已经记录过了

#include <bits/stdc++.h>
using namespace std;
typedef unsigned long long ull;
const int maxn = 1e5 + 5;
const int seed = 31;
ull _base[maxn],_hash[maxn];
map<ull,ull> mp;
int M,L;
char s[maxn];
void init()
{
    _base[0] = 1;
    for(int i = 1; i <= maxn; i++) {
        _base[i] = _base[i - 1] * seed;
    }
}
ull str_hash(int l,int r)
{
    return _hash[r] - _hash[l - 1] * _base[r - l + 1];
}
int main(void)
{

    init();
    while(scanf("%d %d",&M,&L) != EOF) {
        scanf("%s",s + 1);
        int len = strlen(s + 1);
        _hash[0] = 0;
        for(int i = 1; i <= len; i++) {
            _hash[i] = _hash[i - 1] * seed + (s[i] - 'a');
        }
        int ans = 0;
        for(int i = 1; i <= L && i + M * L <= len; i++) {
            mp.clear();
            for(int j = i; j < i + M * L; j += L) {
                mp[str_hash(j,j + L - 1)]++;
            }
            if(mp.size() == M) {
                ans++;
            }
            for(int j = i + M * L; j <= len - L + 1; j += L) {
                mp[str_hash(j - M * L,j - M * L + L - 1)]--;
                if(mp[str_hash(j - M * L,j - M * L + L - 1)] == 0) {
                    mp.erase(str_hash(j - M * L,j - M * L + L - 1));
                }
                mp[str_hash(j,j + L - 1)]++;
                if(mp.size() == M) {
                    ans++;
                }
            }
        }
        printf("%d\n",ans);
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值