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种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一个需要的魔咒,所以他需要你的帮助. 给你一部魔咒词 ...
随机推荐
- SG博弈函数模板
下面这两个模版应该就比较严密了,这个里边的f[]是从零开始的. 转载出处:转自:http://blog.csdn.net/primoblog/article/details/13376057 1.sg ...
- 九度oj 题目1104:整除问题
题目描述: 给定n,a求最大的k,使n!可以被a^k整除但不能被a^(k+1)整除. 输入: 两个整数n(2<=n<=1000),a(2<=a<=1000) 输出: 一个整数. ...
- Java算法HmacSHA256不可用 - Java Algorithm HmacSHA256 not available
发布的jar包,运行后发现报错 Java Algorithm HmacSHA256 not available 百般思索,发现原生JAR包问题 解决办法: Copy sunjce_provider.j ...
- Redis的数据类型及相关操作命令
redis 基础内容 —— redis的数据类型及相关操作的Linux命令.所谓大厦千层基础承载,希望大家认真学习这一讲: 一.redis 的五大数据类型: 1.String(字符串): 2.List ...
- 洛谷P1435 回文字串
题目背景 IOI2000第一题 题目描述 回文词是一种对称的字符串.任意给定一个字符串,通过插入若干字符,都可以变成回文词.此题的任务是,求出将给定字符串变成回文词所需要插入的最少字符数. 比如 “A ...
- Spring JdbcTemplate 查询方法中的RowMapper实现汇总
实现一.在内部建立内联类实现RowMapper接口 package hysteria.contact.dao.impl; import java.sql.ResultSet; import java. ...
- *AtCoder Regular Contest 094 F - Normalization
$n \leq 200000$的abc字符串,现能进行如下变换零次或若干次:选一个$i<n$且$s_i \neq s_{i+1}$,把$s_i$和$s_{i+1}$替换成abc三个字母中除了这两 ...
- 【Tomcat】解决Tomcat catalina.out 不断成长导致档案过大的问题
Tomcat的网站上的说法http://wiki.apache.org/tomcat/FAQ/Logging#Q6: System.out 和 System.err 都被打印到 catalina.ou ...
- Mysql Binlog日志文件介绍
一.Binlog简介 官方文档参考 https://dev.mysql.com/doc/refman/5.5/en/binary-log.html Binlog(Binary Log) 指数据库的表创 ...
- UOJ 41. 矩阵变换
Discription 给出一个 N 行 M 列的矩阵A, 保证满足以下性质: 1.M>N. 2.矩阵中每个数都是 [0,N] 中的自然数. 3.每行中, [1,N] 中 ...