题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4821

题目大意:给你M,L两个字母,问你给定字串里不含M个长度为L的两两相同的子串有多少个?

哈希+枚举

我就是不会枚举这样的,这次涨姿势了。

每次枚举起点,然后就能枚举全部的。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <map>
#include <set>
using namespace std;
typedef unsigned long long ull; const ull B = **+;
int M,L;
char s[];
ull h[];
ull base[];
ull tt[]; int main(){
base[] = ;
for(int i=;i<;i++) base[i] = base[i-]*B;
while(scanf("%d%d",&M,&L)!=EOF){
memset(h,,sizeof(h));
scanf("%s",s);
int len = strlen(s);
for(int i=;i<len;i++){
if( i== ) h[i] = s[i];
else h[i] = h[i-]*B + s[i];
}
int ans = ;
// printf("len = %d\n",len);
for(int i=;i<L;i++){ // 枚举全部起点
// puts("*******************");
int cnt = ;
map<ull,int> H;
for(int j=i;j+L<=len;j+=L){ // 枚举每段
tt[cnt++] = h[j+L-] - (j==?:(h[j-]*base[L]));
// printf("%d => %llu\n",j,tt[cnt-1]);
}
// printf("cnt=%d\n",cnt);
// 迟取法取每段然后数数,注意这里的j<min(cnt,M)
for(int j=;j<min(cnt,M);j++){
// printf("******%llu\n",H[tt[j]]);
H[tt[j]]++;
}
if( H.size()==M ){
ans++;
// printf("%d==%d\n",H.size(),M);
}
for(int j=M;j<cnt;j++){
H[tt[j-M]]--;
if( H[tt[j-M]]== ) H.erase(tt[j-M]);
H[tt[j]]++;
if( H.size()==M ){
ans++;
// printf("%d==%d\n",H.size(),M);
}
}
}
printf("%d\n",ans);
}
return ;
}

[HDU 4821] String (字符串哈希)的更多相关文章

  1. HDU 4821 String 字符串hash

    String Problem Description   Given a string S and two integers L and M, we consider a substring of S ...

  2. HDU 3973 AC's String 字符串哈希

    HDU 3973 通过哈希函数将一个字符串转化为一个整数,通过特定的方式可以使得这个哈希值几乎没有冲突(这就是关键之处,几乎没有视为没有= =!, 其实也可以考虑实现哈希冲突时的处理,只是这道题没必要 ...

  3. HDU 4821 String(BKDRHash)

    http://acm.hdu.edu.cn/showproblem.php?pid=4821 题意:给出一个字符串,现在问你可以找出多少个长度为M*L的子串,该子串被分成L个段,并且每个段的字符串都是 ...

  4. HDU 4821 String(2013长春现场赛I题)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4821 字符串题. 现场使用字符串HASH乱搞的. 枚举开头! #include <stdio.h ...

  5. HDU 4821 String (HASH)

    题意:给你一串字符串s,再给你两个数字m l,问你s中可以分出多少个长度为m*l的子串,并且子串分成m个长度为l的串每个都不完全相同 首先使用BKDRHash方法把每个长度为l的子串预处理成一个数字, ...

  6. HDU 4821 String hash

    String Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  7. 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 ...

  8. TTTTTTTTTTTTTTTT hdu 5510 Bazinga 字符串+哈希

    Bazinga Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Sub ...

  9. Hdu 4821 (字符串hash+map)

    题目链接https://vjudge.net/problem/HDU-4821 题意:给定字符串S ,询问用几个子串满足 : 1.长度为n*len  . 2. n个子串都不相同. 题解:倒序hash将 ...

随机推荐

  1. 解决outlook不能显示鼠标问题

    今天发现打开outlook2010后, 没有鼠标显示. 解决方案: Control Panel -> Mouse Settings ->Pointer Options Uncheck th ...

  2. 从K近邻算法谈到KD树、SIFT+BBF算法

    转自 http://blog.csdn.net/v_july_v/article/details/8203674 ,感谢july的辛勤劳动 前言 前两日,在微博上说:“到今天为止,我至少亏欠了3篇文章 ...

  3. Kettle使用jndi mssqlserver

    kettle可以使用jdbc的方式设置job或者tansform的数据库连接,但是,同时它也支持JNDI方式连接数据库,后者更加方便,只需要配置一份配置文件就可以了,不用每个DBConnection都 ...

  4. Linux下Memcached-1.4.10安装

    memcache是一款流行的缓存产品,它分为两个部分:一个是运行在服务器端的memcached进程,一个是在客户端进行调用获取缓存中数据客户端,例如比较常用的PHP客户端.这里,记录一下安装服务器端的 ...

  5. VBA 插入一行保留样式

    Rows(processingRow).Insert ' 在指定的行数processingRow处插入一行 Rows(processingRow - 1).Select ' 选择上一行的整行 Sele ...

  6. Winform用匿名方法新建线程的方法

    作用:1.将耗时的操作放在单独的线程,加快UI的响应速度.Thread t = new Thread(delegate() { parse.ParseDay(StockCode, FileName); ...

  7. [Hibernate] - Fetching strategies

    Hibernate中的抓取策略,参考文档: http://docs.jboss.org/hibernate/orm/3.5/reference/zh-CN/html/performance.html ...

  8. 【maven】之使用jetty发布web项目

    <plugin> <groupId>org.eclipse.jetty</groupId> <artifactId>jetty-maven-plugin ...

  9. sqlite3移植到arm linux

    1,环境: 软件:linux:2.6.38 硬件:6410 交叉编译工具:arm-linux-gcc 也适用于其他linux平台. 2,步骤: 1>下载sqlite3源码包: http://ww ...

  10. application in 2014

    OA WEBSITE ERP ISO ANDROID EF+MVC4+CATCHE+JQuery+js+div+css+web性能优化+webservice+sql2008+设计模式+wcf+多线程 ...