HDU 4821 String hash
String
Time Limit: 1 Sec
Memory Limit: 256 MB
题目连接
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=95149#problem/I
Description
(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
Sample Input
3 3 abcabcbcaabc
Sample Output
2
HINT
题意
给你一个字符串,让你找到有多少个长度为m*l的子串,由m个长度为l的不同的串构成的
题解:
hash一下之后,就直接暴力找就好了
暴力得用类似滑块一样优化一下就好了
代码:
#include<stdio.h>
#include<iostream>
#include<cstring>
#include<map>
using namespace std;
typedef long long ll;
#define maxn 100005
ll h[maxn*];
ll h2[maxn*];
char str[maxn*];
int n,len,k,s1,s3,vis[maxn],sum[maxn];
ll N=;
ll p=;
ll powp[maxn*]; void get_hash()
{
h[]=(ll)str[];
for(int i=;i<n;i++)
{
h[i]=(h[i-]*p+(ll)str[i]);
while(h[i]<)
h[i]+=N;
if(h[i]>=N)
h[i]%=N;
}
powp[]=1LL;
for(int i=;i<n;i++)
{
powp[i]=powp[i-]*p;
while(powp[i]<)
powp[i]+=N;
if(powp[i]>=N)
powp[i]%=N;
}
}
ll gethash(int l,int r)
{
if(!l)
return h[r];
ll ans=h[r]-h[l-]*powp[r-l+];
if(ans<)
ans%=N;
if(ans<)
ans+=N;
if(ans>=N)
ans%=N;
return ans;
} map<ll ,int> H;
int main()
{
int M,L;
while(scanf("%d%d",&M,&L)!=EOF)
{
memset(vis,,sizeof(vis));
memset(sum,,sizeof(sum));
scanf("%s",str);
len = strlen(str);
n = len;
get_hash();
/*
int ans = len-M*L+1;
for(int i=0;i<L;i++)
{
H.clear();
int flag=0;
for(int j=0;i+(j+1)*L<=len;j++)
{
ll pp=gethash(i+j*L,i+(j+1)*L-1);
if(H[pp])
{
vis[i+(H[pp]-1)*L]=1;
vis[i+j*L]=1;
}
H[pp]=j+1;
}
if(vis[i])sum[i]=1;else sum[i]=0;
for(int j=1;i+j*L+L<=len;j++)
{
sum[i+j*L]=sum[i+(j-1)*L];
if(vis[i+j*L])
{
sum[i+j*L]++; }
if(i+M*L<=len&&sum[i+(M-1)*L]!=0) ans--;
for(int j=M;i+j*L+L<=len;j++)
if(sum[i+(j-M)*L]!=sum[i+j*L]) ans--;
}
*/
int ans = ;
for(int i=;i<L;i++)
{
H.clear();
for(int j=;j<M&&i+(j+)*L-<len;j++)
{
//cout<<i+j*L<<" "<<i+(j+1)*L-1<<" 1"<<" "<<gethash(i+j*L,i+(j+1)*L-1)<<endl;
H[gethash(i+j*L,i+(j+)*L-)]++;
}
//cout<<H.size()<<endl;
if(H.size()==M)ans++;
for(int j=M;i+(j+)*L-<len;j++)
{
//cout<<i+j*L<<" "<<i+(j+1)*L-1<<" 2"<<" "<<gethash(i+(j-M)*L,i+(j+1-M)*L-1)<<endl;
H[gethash(i+(j-M)*L,i+(j+-M)*L-)]--;
if(H[gethash(i+(j-M)*L,i+(j+-M)*L-)]==)
H.erase(gethash(i+(j-M)*L,i+(j+-M)*L-));
H[gethash(i+j*L,i+(j+)*L-)]++;
//cout<<H.size()<<endl;
if(H.size()==M)ans++;
}
}
printf("%d\n",ans);
}
}
HDU 4821 String hash的更多相关文章
- hdu 4821 字符串hash+map判重 String (长春市赛区I题)
http://acm.hdu.edu.cn/showproblem.php?pid=4821 昨晚卡了非常久,開始TLE,然后优化了之后,由于几个地方变量写混.一直狂WA.搞得我昨晚都失眠了,,. 这 ...
- HDU 4821 String (HASH)
题意:给你一串字符串s,再给你两个数字m l,问你s中可以分出多少个长度为m*l的子串,并且子串分成m个长度为l的串每个都不完全相同 首先使用BKDRHash方法把每个长度为l的子串预处理成一个数字, ...
- 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 字符串hash
String Problem Description Given a string S and two integers L and M, we consider a substring of S ...
- 2013 Asia Regional Changchun I 题,HDU(4821),Hash
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4821 解题报告:搞了很久,总算搞出来了,还是参考了一下网上的解法,的确很巧,和上次湘潭的比 ...
- HDU 4821 String(2013长春现场赛I题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4821 字符串题. 现场使用字符串HASH乱搞的. 枚举开头! #include <stdio.h ...
- HDU 4821 String(BKDRHash)
http://acm.hdu.edu.cn/showproblem.php?pid=4821 题意:给出一个字符串,现在问你可以找出多少个长度为M*L的子串,该子串被分成L个段,并且每个段的字符串都是 ...
- [HDU 4821] String (字符串哈希)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4821 题目大意:给你M,L两个字母,问你给定字串里不含M个长度为L的两两相同的子串有多少个? 哈希+枚 ...
- HDU 4821 字符串hash
题目大意: 希望找到连续的长为m*l的子串,使得m个l长的子串每一个都不一样,问能找到多少个这样的子串 简单的字符串hash,提前预处理出每一个长度为l的字符串的hash值 #include < ...
随机推荐
- JQuery实现——黑客帝国代码雨效果
效果如你所见就是本页面上方那样的效果 实现方法来自一个印度小伙纸,学习完我也没总结一下,今儿个补上 如何实现,大家右键查看源码复制即可,不过学习的过程还是要总结总结. 下面通过另外两个小例子,一步一步 ...
- 【转】Android 如何在Eclipse中查看Android API源码 及 support包源码
原文网址:http://blog.csdn.net/vipzjyno1/article/details/22954775 当我们阅读android API开发文档时候,上面的每个类,以及类的各个方法都 ...
- Oracle 跟踪事件 set event
一.Oracle跟踪文件 Oracle跟踪文件分为三种类型,一种是后台报警日志文件,记录数据库在启动.关闭和运行期间后台进程的活动情况,如表空间创建.回滚段创建.某些alter命令.日志切换.错误消息 ...
- delete drop truncate
一.相同点 1 truncate.不带where子句的delete.drop都会删除表内的数据2 drop.truncate都是DDL语句(数据定义语言),执行后会自动提交 二.不同点 1trunca ...
- Delphi word 颜色
unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, ...
- 四款超棒的jQuery数字化签名插件
在浏览器中,我们有很多方式来绘制生成签名效果,并且有很多很棒很智能的jQuery插件.数字化签名是未来的发展方向,正是这个原因我们这里收集并且推荐了四款超棒的jQuery数字化签名插件,希望大家喜欢! ...
- 关于“心脏出血”漏洞(heartbleed)的理解
前阵子“心脏出血”刚发生的时候读了下源代码,给出了自己觉得比较清楚的理解. -------------------------穿越时空的分割线--------------------------- ...
- 解决PHP5.3.x下ffmpeg安装配置问题
本人的环境: OS : windows 7 64位 WAMP:2.1a PHP:5.3.3(之前是5.3.13) 项目需要用ffmpeg-php实现上传视频转码截图等功能,但是找了很多资料都没有把ff ...
- Cloudera 建议使用 NTP 使 Hadoop 群集实现时间同步
主机的 NTP 服务未响应时钟偏差请求. 建议 这是主机运行状况测试,用于检查主机的系统时钟是否与其 NTP 服务器不同步.该测试能检查“ntpdc -c loopinfo”命令报告的主机时钟偏差绝对 ...
- Spark系列(五)Master主备切换机制
Spark Master主备切换主要有两种机制,之中是基于文件系统,一种是基于Zookeeper.基于文件系统的主备切换机制需要在Active Master挂掉后手动切换到Standby Master ...