String
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 248 Accepted Submission(s): 83
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
字符串处理题,给定一个字符串,询问有多少个连续子串,其满足:
1.长度为L*M且连续
2.将该子串按顺序切割为M份,每份长度为L,且每个部分均不相同。
这一题用到了字符串hash的技巧
采用BKDhash处理较为方便(类似多项式hash),设s[0]到s[l-1]的hash值为a[l]
则对于s[l]到s[l+k-1]一段的hash值为a[l+k-1]-s[l-1]*base[k]
对于每个开头按顺序处理一次,用map记录是否出现重复的hash即可。
unsigned long long base[100005];
unsigned long long pos[100005];
unsigned long long shash[100005];
map <unsigned long long,int>hash_table;
main()
{
int m,l,n,k;
base[0]=1;
for (int i=1;i<=100000;i++)
base[i]=base[i-1]*131;
while(~scanf("%d%d",&m,&l)){
pos[0]=0;
scanf("%s",a);
n=strlen(a);
for (int i=0;i<n;i++)
pos[i+1]=pos[i]*131ull+a[i];
for (int i=1;i<=n-l+1;i++)
shash[i]=pos[i+l-1]-pos[i-1]*base[l];//计算每个长度为l的hash
int fp,lp,ti,ans=0;
for (int i=1;(i<=l)&&i+l-1<=n;i++)
{
ti=0;
hash_table.clear();
fp=i;
lp=i+l*(m-1);//fp代表开头 lp代表结尾
if (lp+l-1>n) break;
for (int j=fp;j<=lp;j+=l)
{
if (hash_table[shash[j]]==0) ++ti;
hash_table[shash[j]]++;
}
if (ti==m) ans++;
while (lp+l+(l-1)<=n)
{
if (hash_table[shash[fp]]==1) --ti;
hash_table[shash[fp]]--;
fp+=l;
lp+=l;
if (lp>n) break;
if (hash_table[shash[lp]]==0) ++ti;
hash_table[shash[lp]]++;
if (ti==m) ans++;
}
}
printf("%d\n",ans);
}
}
题意:
给定整数M L
一个字符串s
我们定义一个子串为"好"串 iff
1、长度为 M*L
2、把这个好串分成M段,每段长度为L,且每段各不相同。
且我们得到的这些好串不重复计算(即把这些好串去重)
问有几个好串
typedef unsigned long long ull;
const int N = 100005;
int n, m;
char s[N];
ull base[N], has[N], bas = 31;
map<ull, int>mp;
int main(){
base[0] = 1;
for(int i = 1; i < N; i++)base[i] = base[i-1]*bas;
while(cin>>n>>m){
scanf("%s", s);
int slen = strlen(s);
has[slen] = 0;
for(int i = slen-1; i >= 0; i--)
has[i] = has[i+1]*bas+s[i]-'a'+1;
int ans = 0;
for(int i = 0; i < m && i+n*m<=slen; i++)
{
mp.clear();
for(int j = i; j < i+n*m; j+=m)
{
mp[ has[j] - has[j+m]*base[m] ] ++;
}
ans += mp.size() == n;
for(int j = i+n*m; j+m <= slen; j+=m){
ull tmp = has[j-n*m] - has[j-(n-1)*m]*base[m];
mp[tmp] --;
if(mp[tmp]==0)mp.erase(tmp);
tmp = has[j] - has[j+m]*base[m];
mp[tmp]++;
ans += mp.size() == n;
}
}
cout<<ans<<endl;
}
return 0;
}