题目链接: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. PorterDuff.Mode error

    Android PorterDuff.Mode error: PorterDuff cannot be resolved to a variable     Answers:   Add this t ...

  2. 利用Java Service Wrapper将java项目添加到windows服务中

    1.web项目,即tomcat/resin添加至window系统服务,步骤如下:第一步:找到tomcat的bin目录,如:D:\apache-tomcat-8.0.26\bin第二步:打开cmd,cd ...

  3. php 导出csv文件

    <?php $sql = "select * from members_sqzj order by id asc"; $result = $db->fetch_All( ...

  4. FIR系统的递归与非递归实现

    首先,因为FIR的脉冲响应是有限长,所以总是可以非递归实现的: 其次,也可以用递归系统来实现它. 以滑动平均做例子,最直观的想法就是,每次来一个新的值,丢掉最老的,加上最新的: y[n]=y[n-1] ...

  5. Xshell远程连接Linux时无法使用小键盘的解决方式

    我在用xshell连接远程的centos时,每次使用vi/vim的时候而NumLock明明在开启着,小键盘都不能正确输入数字,其实这是时按小而是出现一个字母然后换行(实际上是命令模式上对应上下左右的键 ...

  6. 借助HTML分别禁用IE8, IE9的兼容视图模式的小技巧

    IE 添加了兼容模式,开启后会以低一版本的 IE 进行渲染,但是我就遇到了一种情况,在 IE8 下只有不使用兼容模式页面才能显示正常,下面有个不错的方法可以解决这个问题 从 IE 8 开始,IE 添加 ...

  7. [svn]svn conflict 冲突解决

    转自:http://www.gezila.com/tutorials/17290.html 目录: 1. 同一处修改文件冲突 1.1. 解决方式一 1.2. 解决方式二 1.3. 解决总结 2. 手动 ...

  8. 【转】 远程到服务器安装visualSVN server,出现Service 'VisualSVN Server' failed to start的解决方法

    在帮助远程到服务器上安装visualSVN server的时候,出现Service 'VisualSVN Server' failed to start. 解决方法(先不要关闭安装弹出的错误窗口): ...

  9. ios8 ios7 tableview cell 分割线左对齐

    ios8中左对齐代码 //加入如下代码 -(void)tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cel ...

  10. PO_PO系列 - 收货管理分析(案例)

    2014-07-01 Created By BaoXinjian