串hash

String

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 697    Accepted Submission(s): 190

Problem Description
Given a string S and two integers L and M, we consider a substring of S as “recoverable” if and only if

  (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
For each test case, output the answer in a single line.
 
Sample Input
3 3
abcabcbcaabc
 
Sample Output
2
 
Source
 

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map> using namespace std; typedef unsigned long long int ull; const int maxn=100100; int L,M;
char str[maxn]; ull xp[maxn],hash[maxn];
map<ull,int> ck; void init()
{
xp[0]=1;
for(int i=1;i<maxn;i++)
xp[i]=xp[i-1]*175;
} ull get_hash(int i,int L)
{
return hash[i]-hash[i+L]*xp[L];
} int main()
{
init();
while(scanf("%d%d",&M,&L)!=EOF)
{
scanf("%s",str);
int n=strlen(str);
hash[n]=0;
for(int i=n-1;i>=0;i--)
{
hash[i]=hash[i+1]*175+(str[i]-'a'+1);
}
int ans=0;
for(int i=0;i<L;i++)
{
ck.clear();
int duan=0;
for(int j=0;i+(j+1)*L-1<n;j++)
{
/// i+j*L <---> i+(j+1)*L-1
duan++;
ull hahashsh=get_hash(i+j*L,L);
ck[hahashsh]++;
if(duan>=M)
{
if(duan>M)
{
/// M+1 ago : i+(j+1)*L-L*(M+1)
ull Mago=get_hash(i+(j+1)*L-L*(M+1),L);
if(ck[Mago])
{
ck[Mago]--;
if(ck[Mago]==0) ck.erase(Mago);
}
}
if(ck.size()==M) ans++;
}
}
}
printf("%d\n",ans);
}
return 0;
}

版权声明:本文博客原创文章。博客,未经同意,不得转载。

HDOJ 4821 String的更多相关文章

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

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

  2. HDU 4821 String (HASH)

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

  3. [HDU 4821] String (字符串哈希)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4821 题目大意:给你M,L两个字母,问你给定字串里不含M个长度为L的两两相同的子串有多少个? 哈希+枚 ...

  4. HDU 4821 String hash

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

  5. HDU 4821 String(BKDRHash)

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

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

  7. HDU 4821 String 字符串hash

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

  8. 【HDOJ】3553 Just a String

    后缀数组加二分可解. /* 3553 */ #include <iostream> #include <sstream> #include <string> #in ...

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

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

随机推荐

  1. poj 2299 树状数组求逆序数+离散化

    http://poj.org/problem?id=2299 最初做离散化的时候没太确定可是写完发现对的---由于后缀数组学的时候,,这样的思维习惯了吧 1.初始化as[i]=i:对as数组依照num ...

  2. hdu1507--二分图最大匹配

    题意:你大爷.哦不! 你大叔继承了一块地什么的都是废话..,这里说说题意,和怎么建图. 题意:这里有一块N*M的地,可是有 K 个地方.是池塘,然后输入K行(x,y),OK,如今能够出售的地必须是 1 ...

  3. 由sqlite在手机的内存位置,引起onCreate当运行总结

    转载请注明出处.谢谢:http://blog.csdn.net/harryweasley/article/details/46467495 我们都知道,android为了操作数据库,通常是继承SQLi ...

  4. Effective C++:条款38:通过一个复杂的模具has-a要么“基于一些实现”

    (一) public继承是"is-a"关联,"has-a"或"依据某物实现出(is-implemented-in-terms-of)"的意思 ...

  5. WebApi异常

    WebApi异常处理解决方案   前言:上篇C#进阶系列——WebApi接口传参不再困惑:传参详解介绍了WebApi参数的传递,这篇来看看WebApi里面异常的处理.关于异常处理,作为程序员的我们肯定 ...

  6. Mac OS X在建筑Python科学计算环境

    经验(比如这篇日志:http://blog.csdn.net/waleking/article/details/7578517).他们推荐使用Mac Ports这种软件来管理和安装全部的安装包.依照这 ...

  7. Hadoop-2.2.0中国文献——MapReduce 下一代 —配置单节点集群

    Mapreduce 包 你需从公布页面获得MapReduce tar包.若不能.你要将源代码打成tar包. $ mvn clean install -DskipTests $ cd hadoop-ma ...

  8. Python 的PyCurl模块使用

    PycURl是一个C语言写的libcurl的python绑定库.libcurl 是一个自由的,并且容易使用的用在客户端的 URL 传输库.它的功能很强大,PycURL 是一个非常快速(参考多并发操作) ...

  9. silverlight与wcf双向通讯 例子

    本文将建立一个silverlight与wcf双向通讯的简单实例,以下是详细步骤: 新建Silverlight应用程序,名称WCFtest.解决方案中添加WCF服务应用程序,名称WcfServiceTe ...

  10. android手机SD卡中的android_secure目录

    .android_secure 是官方app2sd的产物,删了之后装到sd卡中的软件就无法使用了.里面有非常多.asec的文件,都是装到SD卡上的软件,可是一般装到sd卡中的程序被卸载了.androi ...