Hdu 4821 String

hash大法好。。。

#include<stdio.h>
#include<string.h>
#include<map>
#include<math.h>
#include<algorithm>
using namespace std;

#define rep(i,s,t) for(int i=s;i<t;i++)
typedef unsigned long long ull;
const ull B=100000007;
int m,l,len;
char s[100005];
ull ll[100005];
ull a,t;
ull b[100005],c[100005];
int cnt;
map<ull,bool>mp;

inline int solve(int i,int len){
	mp.clear();
	cnt=0;
	int ans=0;
	for(int j=i;j<len;j+=l){
		b[cnt++]=ll[j];
	}
	//rep(j,0,cnt) printf("%llu\n",b[j]);
	int s=0,t=0;
	rep(j,0,cnt){
		c[t++]=b[j];
		if(mp[b[j]]){
			while(s<t && c[s]!=b[j]) mp[c[s++]]=0;
			mp[c[s++]]=0;
		}	
		mp[b[j]]=1;
		while(t-s>m) mp[c[s++]]=0;
		//printf("%d %d\n",t,s);
		if(t-s==m) ans++;
	}
	return ans;
}
int main(){
	while(~scanf("%d%d",&m,&l)){
		scanf("%s",s);len=strlen(s);
		t=1;rep(i,0,l) t*=B;
		a=0;rep(i,0,l) a=a*B+s[i];
		ll[l-1]=a;
		rep(i,l,len) ll[i]=ll[i-1]*B+s[i]-s[i-l]*t;
		int ans=0;
		rep(i,l-1,l*2-1){
			ans+=solve(i,len);
			//printf("\n");
		}
		printf("%d\n",ans);
	}	
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值