HDU4821 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。问最多能找出多少个符合要求的子串。
要求:1.该子串长度为m*l。2.将该子串分割为m个长度为l的子串,要求这m个子串互不相同。

题目分析

这个题思路其实也不难。枚举s中所有长度为m*l的子串。
再将这个子串分割为m个长度为l的字符串,再通过map+字符串哈希判断这m个子串中有没有重复。

但这样做的时间复杂度过高,因此我们还需要进一步优化:
当枚举某一个位置的m*l字符串时,当判断完毕之后,我们可以先不向后枚举,而是删去开头的长度为l的子串,而在字符串末尾加上后面的一个长度为l的子串。这样就是一种新的情况,重复这个操作直到字符串后面剩的字符不到l个为止。
这样就可以把外圈循环的次数控制在l之内了。
在判断过程中将所有符合条件的记录下来,最后输出即可。

代码如下
#include <iostream>
#include <cmath>
#include <cstdio>
#include <set>
#include <string>
#include <cstring>
#include <map>
#include <unordered_map>
#include <algorithm>
#include <queue>
#define ULL unsigned long long
#define PII pair<int,int>
#define x first
#define y second
using namespace std;
const int N=1e5+5,P=131;
char s[N];
ULL p[N],h[N];
int main()
{
	p[0]=1;
	for(int i=1;i<N;i++) p[i]=p[i-1]*P;
	int m,l;
    while(~scanf("%d%d",&m,&l))
    {
    	scanf("%s",s+1);
    	int n=strlen(s+1);
		for(int i=1;i<=n;i++) h[i]=h[i-1]*P+s[i];
		
		int ans=0;
		for(int st=1;st<=l;st++)			//枚举起点(st=l+1与st=1是重复的,因此只需要枚举到l)
		{
			queue<int> q;					//用队列保证m个长度为l的子串
			unordered_map<ULL,int> f;		//记录每个子串的hashcode
			for(int i=st;i+l-1<=n;i+=l)		//枚举起点
			{
				ULL val=h[i+l-1]-h[i-1]*p[l];	//计算当前段的hashCode
				q.push(i);						//将当前位置放入队列
				if(q.size()>m)					//如果队列中元素大于m,则删除首元素
				{
					int t=q.front();
					q.pop();
					ULL x=h[t+l-1]-h[t-1]*p[l];
					f[x]--;
					if(f[x]==0) f.erase(x);		//如果f[x]==0,那么直接删除该位置
				}
				f[val]++;						//用f记录val
				if(q.size()==m&&f.size()==m) ans++;		//如果f.size()==m,说明f中没有重复的val,答案+1
			}
		}
		printf("%d\n",ans);		//输出答案
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

lwz_159

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值