参考资料:http://blog.csdn.net/ck_boss/article/details/47066727?readlog
String
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3682 Accepted Submission(s): 1110
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.
(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.
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
Recommend
题意:给两个整数L,M和一个字符串,问这个字符串有多少个满足要求的子串。
要求有两个:①子串的长度为L*M
②把子串分为M个长度为L的串以后,这些串不能有两个完全一样的串。
题解:通过字符串hash,可以很轻松地求出任意一个长度为L的子串的hash值。枚举字符串起始位置,这个从0枚举到L-1,然后,在这个位置开始,每L个字符作为一块,首先将前M块插入到map中,同时维护不相同字符串的个数,如果这个数量为M,那么显然是满足要求的。接下来,将这个区间向右移,删掉第1块,加入第M+1块,同样维护那个值。
#include<cstdio>
#include<iostream>
#include<map>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxn 100100
typedef unsigned long long int ull;
char str[maxn];
ull xp[maxn];
ull hash1[maxn];
ull base = 175;
map<ull, int>mp;
void init()
{
xp[0] = 1;
for (int i = 1; i < maxn; i++)
xp[i] = xp[i - 1] * base;
}
ull get_hash(int i, int L)
{
return hash1[i] - hash1[i+L] * xp[L];
}
int main()
{
int M, L;
init();
while (scanf("%d%d",&M,&L)!=EOF)
{
scanf("%s", str);
int len = strlen(str);
hash1[len] = 0;
for (int i = len - 1; i >= 0; i--)
{
hash1[i] = hash1[i + 1] * base + (str[i] - 'a'+1);
}
int ans = 0;
for (int i = 0; i < L; i++)
{
mp.clear();
int cnt = 0;
// i + j*L <--->i + (j + 1)*L - 1
for (int j = 0; i + (j+1)*L-1 < len; j++)
{
cnt++;
ull tmp = get_hash(i + j*L,L);
mp[tmp]++;
if (cnt >= M)
{
if (cnt > M)
{
// M+1 ago : i+(j+1)*L-L*(M+1)
ull tmp1 = get_hash(i + (j - M)*L, L);//第前M个
if (mp[tmp1])
{
mp[tmp1]--;
if(mp[tmp1]==0)mp.erase(tmp1);//可能中间有重复
}
}
if (mp.size() == M)ans++;
}
}
}
printf("%d\n", ans);
}
return 0;
}
本文介绍了一种利用字符串Hash技术解决特定子串问题的方法。针对给定长度和多样性的子串要求,通过滑动窗口结合Hash值高效计算满足条件的不同子串数量。
214

被折叠的 条评论
为什么被折叠?



