HDU 4821 String 字符串hash
String
(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.
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.
abcabcbcaabc
题意:
给你M和L,和一个字符串S。
要求找出S的子串中长度为L*M,并且可以分成M段,每段长L,并且M段都不相同的子串个数。
题解:
枚举起点
hash每个前缀串
那么一段子串的hash值就可以快速求出
twopointsO(n)求出长度M*L,,M个连续串是否相同
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<cmath>
#include<map>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
typedef unsigned long long ULL;
const long long INF = 1e18+1LL;
const double pi = acos(-1.0);
const int N = 5e5+, MM = 1e3+,inf = 2e9; const LL mod = 10000019ULL;
inline LL read()
{
LL x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
} map<ULL,int > s;
ULL bhas[N],has[N],sqr[N];
int M,L;
char sa[N];
int vis[N];
int main() {
sqr[] = ;
for(int i = ; i < N; ++i) sqr[i] = sqr[i-] * mod;
while(scanf("%d%d",&M,&L)!=EOF) {
scanf("%s",sa+);
int n = strlen(sa+);
has[] = ;
for(int i = ; i <= n; ++i) {
has[i] = has[i-] * mod + sa[i] - 'a' + ;
}
int ans = ;
for(int i = ; i <= L && i + M * L - <= n; ++i) {
int cnt = ;
s.clear();
int ll = ,rr = ;
for(int j = i; j + L - <= n; j += L) {
int l = j, r = j + L - ;
ULL now = has[r] - has[l-]*sqr[L];
if(s[now] == ){
bhas[++rr] = now;
if(rr - ll + >= M)ans+=;
s[now] = ;
continue;
}
else {
while(ll <= rr && bhas[ll]!=now) {
s[bhas[ll++]] = ;
}
s[bhas[ll++]] = ;
bhas[++rr] = now;
if(rr - ll + >= M)ans+=;
s[now] = ;
}
}
}
printf("%d\n",ans);
}
return ;
}
HDU 4821 String 字符串hash的更多相关文章
- HDU 4821 String (HASH)
题意:给你一串字符串s,再给你两个数字m l,问你s中可以分出多少个长度为m*l的子串,并且子串分成m个长度为l的串每个都不完全相同 首先使用BKDRHash方法把每个长度为l的子串预处理成一个数字, ...
- [HDU 4821] String (字符串哈希)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4821 题目大意:给你M,L两个字母,问你给定字串里不含M个长度为L的两两相同的子串有多少个? 哈希+枚 ...
- Hdu 4821 (字符串hash+map)
题目链接https://vjudge.net/problem/HDU-4821 题意:给定字符串S ,询问用几个子串满足 : 1.长度为n*len . 2. n个子串都不相同. 题解:倒序hash将 ...
- HDU 4821 String(2013长春现场赛I题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4821 字符串题. 现场使用字符串HASH乱搞的. 枚举开头! #include <stdio.h ...
- hdu 4622 Reincarnation 字符串hash 模板题
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4622 题意:给定一个长度不超过2000的字符串,之后有不超过1e5次的区间查询,输出每次查询区间中不同 ...
- HDU 4821 String hash
String Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.action ...
- HDU - 4821 String(窗口移动+map去重+hash优化)
String Given a string S and two integers L and M, we consider a substring of S as “recoverable” if a ...
- HDU 4821 String(BKDRHash)
http://acm.hdu.edu.cn/showproblem.php?pid=4821 题意:给出一个字符串,现在问你可以找出多少个长度为M*L的子串,该子串被分成L个段,并且每个段的字符串都是 ...
- 魔咒词典 HDU - 1880 (字符串hash 单hash转int或者 双hash )
哈利波特在魔法学校的必修课之一就是学习魔咒.据说魔法世界有100000种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一个需要的魔咒,所以他需要你的帮助. 给你一部魔咒词 ...
随机推荐
- Charlie's Change(完全背包记录路径)
Charlie is a driver of Advanced Cargo Movement, Ltd. Charlie drives a lot and so he often buys coffe ...
- 网页QQ唤起
网页QQ唤起 <html> <head> <meta http-equiv="Content-Type" content="text/htm ...
- MySQL5.7 服务 crash 后无法启动
事发背景 测试环境更换数据盘,直接采取在线将数据目录暴力拷贝到新盘,然后将原服务关闭,启用新盘. 服务是可以正常启动的,但是没多会开发就反应服务down了,错误日志输出 -- :: InnoDB: F ...
- Git 撤消操作
修改最后一次提交 有时候我们提交完了才发现漏掉了几个文件没有加,或者提交信息写错了.想要撤消刚才的提交操作,可以使用 --amend 选项重新提交: $ git commit --amend 此命令将 ...
- 【Luogu】P3455Zip-Queries(莫比乌斯反演)
题目链接 真是神TM莫比乌斯 首先来看一个神奇的结论:求gcd(x,y)==k的对数,其中1<=x<=n,1<=y<=m 等同于求gcd(x,y)==1的对数,其中1<= ...
- 【Luogu】P1383高级打字机
可持久化线段树模板题之一. 权当温习主席树模板 #include<cstdio> #include<cstdlib> #include<cctype> #defin ...
- [BZOJ2287]【POJ Challenge】消失之物(DP)
传送门 f[i][j]表示前i个物品,容量为j的方案数c[i][j]表示不选第i个物品,容量为j的方案数两个数组都可以压缩到一维 那么f[i][j] = f[i - 1][j] + f[i - 1][ ...
- 【组合数+Lucas定理模板】HDU 3037 Saving
acm.hdu.edu.cn/showproblem.php?pid=3037 [题意] m个松果,n棵树 求把最多m个松果分配到最多n棵树的方案数 方案数有可能很大,模素数p 1 <= n, ...
- Spoj-ANTP Mr. Ant & His Problem
Mr. Ant has 3 boxes and the infinite number of marbles. Now he wants to know the number of ways he c ...
- spring和mybatis整合配置文件
查看所有springmvc spring mybatis配置文件见下链接: https://my.oschina.net/sherwayne/blog/262616 <?xml versio ...