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 < ...
随机推荐
- 【JSP】JSP检查字符串是否为数字
//判断是否是正整数 function IsNum(s) { if(s!=null){ var r,re; re = /\d*/i; //\d表示数字,*表示匹配多个数字 r = s.match(re ...
- 【转】win7(windows7)下java环境变量配置方法
原文网址:http://jingyan.baidu.com/article/925f8cb836b26ac0dde0569e.html win7(windows7)下java环境变量配置方法,java ...
- HDU 5389 Zero Escape
题意:有一些人,每人拿一个号码,有两个门,门的值分别为A和B,要求把人分成两堆(可以为空)一堆人手持号码之和的数字根若等于A或者B就可以进入A门或者B门,要求两堆人分别进入不同的门,求有几种分配方式, ...
- java文件过滤器
java中有一个FilenameFilter的接口,能够过滤得到指定类型的文件或者目录,其中必须重写accept(File file,String path)方法 public class DirFi ...
- PHP学习笔记--文件目录操作(文件上传实例)
文件操作是每个语言必须有的,不仅仅局限于PHP,这里我们就仅用PHP进行讲解 php的文件高级操作和文件上传实例我放在文章的最后部分.--以后我还会给大家写一个PHP类似于网盘操作的例子 注意:阅读此 ...
- 【C++对象模型】构造函数语意学之二 拷贝构造函数
关于默认拷贝构造函数,有一点和默认构造函数类似,就是编译器只有在[需要的时候]才去合成默认的拷贝构造函数. 在什么时候才是[需要的时候]呢? 也就是类不展现[bitwise copy semantic ...
- bzoj 3629 [JLOI2014]聪明的燕姿(约数和,搜索)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=3629 [题意] 给定S,找出所有约数和为S的数. [思路] 若n=p1^a1*p2^a ...
- 【暑假】[实用数据结构]UVa11995 I Can Guess the Data Structure!
UVa11995 I Can Guess the Data Structure! 思路:边读边模拟,注意empty的判断! 代码如下: #include<iostream> #inclu ...
- 分布式文件系统-HDFS
HDFS Hadoop的核心就是HDFS与MapReduce.那么HDFS又是基于GFS的设计理念搞出来的. HDFS全称是Hadoop Distributed System.HDFS是为以流的方式存 ...
- fedora 安装python mysql
如果你服务器环境允许yum,安装MySQL-python模块就很简单了. 如果直接安装不行,先 安装MySQL-devel后正常运行 yum install mysql-devel yum insta ...