题目大意:

希望找到连续的长为m*l的子串,使得m个l长的子串每一个都不一样,问能找到多少个这样的子串

简单的字符串hash,提前预处理出每一个长度为l的字符串的hash值

 #include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <map>
#include <set> using namespace std;
#define N 100010
#define base 31
typedef unsigned long long ULL;
map<ULL , int> _map;
int m , l;
ULL fac[N] , val[N];
char str[N]; void init()
{
fac[] = ;
for(int i= ; i<= ; i++) fac[i] = fac[i-]*base;
} int main()
{
#ifndef ONLINE_JUDGE
freopen("a.in" , "r" , stdin);
#endif // ONLINE_JUDGE
init();
while(~scanf("%d%d" , &m , &l))
{
scanf("%s" , str);
int len = strlen(str) , cnt , same , ret;
ULL cur = ;
for(int i= ; i<len ; i++){
if(i>=l) cur = cur-fac[l-]*str[i-l];
cur=cur*base+str[i];
if(i>=l-) val[i-l+] = cur;
//cout<<i-l+1<<" "<<val[i-l+1]<<endl;
}
ret=;
for(int i= ; i<l ; i++){
_map.clear();
cnt = , same = ;
for(int j=i ; j<len ; j+=l){
if(j+l->=len) break;
if(cnt >= m){
int la = j-m*l;
int t = _map[val[la]]--;
if(t==) same--;
}
int t = _map[val[j]]++;
if(t==) same++;
cnt++;
// cout<<j<<" "<<cnt<<" "<<endl;
if(cnt >= m && same==){
ret++;
// cout<<j<<endl;
} }
}
printf("%d\n" , ret);
}
return ;
}

HDU 4821 字符串hash的更多相关文章

  1. hdu 4821 字符串hash+map判重 String (长春市赛区I题)

    http://acm.hdu.edu.cn/showproblem.php?pid=4821 昨晚卡了非常久,開始TLE,然后优化了之后,由于几个地方变量写混.一直狂WA.搞得我昨晚都失眠了,,. 这 ...

  2. 2013 Asia Regional Changchun I 题,HDU(4821),Hash

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4821 解题报告:搞了很久,总算搞出来了,还是参考了一下网上的解法,的确很巧,和上次湘潭的比 ...

  3. HDU 4821 String hash

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

  4. hdu 1880 字符串hash

    /*普通的hsah 由于元素太多 空间很小..hash碰撞很厉害.30分*/ #include<iostream> #include<cstdio> #include<c ...

  5. HDU 1880 字符串hash 入门题

    Problem Description 哈利波特在魔法学校的必修课之一就是学习魔咒.据说魔法世界有100000种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一个需要的魔 ...

  6. Hdu 1800 字符串hash

    题目链接 题意: 给出n(n<=3000)个字符串(长度<30,数字组成,肯能存在前导0), 问该序列最少可以分成多少个单调序列.可以转化成求相同字符串的个数的最大值 附上代码: /*** ...

  7. hdu 4622 Reincarnation 字符串hash 模板题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4622 题意:给定一个长度不超过2000的字符串,之后有不超过1e5次的区间查询,输出每次查询区间中不同 ...

  8. HDU 5763 Another Meaning dp+字符串hash || DP+KMP

    题意:给定一个句子str,和一个单词sub,这个单词sub可以翻译成两种不同的意思,问这个句子一共能翻译成多少种不能的意思 例如:str:hehehe   sub:hehe 那么,有**he.he** ...

  9. HDU 4821 String (HASH)

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

随机推荐

  1. solr 常见异常

    solr4.3本地数据提交异常分析 (2013-06-19 16:03:15) 转载▼   异常一. Exception in thread "main" java.lang.No ...

  2. iOS面试题之内存管理

    本文围绕内存管理的几种方法展开叙述. 1.内存管理是什么? 内存管理,就是对内存资源进行优化. 2.内存管理的三种方法? Objective-C的内存管理主要有三种方式ARC(自动内存计数).MRC( ...

  3. f# mathprovider

    http://mathprovider.codeplex.com/ http://mathnetnumerics.codeplex.com/releases/view/110750 http://py ...

  4. hihocoder1067 最近公共祖先·二

    思路: 使用tarjan算法,这是一种离线算法. 实现: #include <bits/stdc++.h> using namespace std; typedef pair<int ...

  5. hihocoder1736 最大的K-偏差排列

    思路: 容易写错的贪心题. 实现: #include <bits/stdc++.h> using namespace std; int main() { int n, k; while ( ...

  6. java实现斐波那契的两种方法

    package com.ywx.count; /** * 斐波那契数列(地推方式要比递归方式的效率要高) * @author Vashon(yangwenxue) * date:20150320 */ ...

  7. redis集群架构(含面试题解析)

    老规矩,我还是以循序渐进的方式来讲,我一共经历过三套集群架构的演进! Replication+Sentinel 这套架构使用的是社区版本推出的原生高可用解决方案,其架构图如下! 这里Sentinel的 ...

  8. PHP环境搭建Zend Studio 10.6.2+WampServer2.4

    址:http://www.zend.com/en/products/studio/downloads直接下载地址:http://downloads.zend.com/studio-eclipse/10 ...

  9. Android(java)学习笔记155:中文乱码的问题处理(qq登录案例)

    1. 我们在之前的笔记中LoginServlet.java中,我们Tomcat服务器回复给客户端的数据是英文的"Login Success","Login Failed& ...

  10. java项目部署jar包

    1. 先将打包成jar包 2. 查看所有的java进程   pgrep java 3. 杀死进程 kill   -9 程序号 4.执行命令  nohup java -jar admin.jar > ...