串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. getline与get函数的区别

    get()函数相对getline来说使用方法要灵活的多了. 1.   int get()是指从流中抽取单个字符并返回,这个是没有參数的形式.由于c++不像c语言使用getchar() 2.istrea ...

  2. 解决方式:QSqlDatabase: an instance of QCoreApplication is required for loading driver plugins

    在用QSqlDatabase时遇到报错QSqlDatabase: an instance of QCoreApplication is required for loading driver plug ...

  3. C++习题 虚函数-计算图形面积

    C++习题 虚函数-计算图形面积 Time Limit: 1 Sec  Memory Limit: 128 MB Submit: 122  Solved: 86 [cid=1143&pid=6 ...

  4. 采用DWR、maven保存数据到数据库

    一.原理: Ajax是时下比较流行的一种web界面设计新思路,其核心思想是从浏览器获取XMLHttp对象与服务器端进行交互. DWR(Direct Web Remoting)就是实现了这种Ajax技术 ...

  5. 【Unity3D自学记录】利用代码改动图片属性(Inspector)

    这段时间一直都在打包资源,然后每次导入都要改图片的属性.真是麻烦,所以一直在寻找一键改动而且打包的方法. 最终让我找到了,太坑人了. 依据自己的需求改代码哦,相信大家都能看明确. 核心部分: Text ...

  6. Android中TweenAnimation四种动画切换效果

    点击每个按钮都会有对应的动画显示 activity代码: package com.tmacsky; import android.app.Activity; import android.os.Bun ...

  7. poj1236 有向图加边变成强连通图

    给我们一个有向图,有两个问题 1.最少要给多少个点发消息,才能使得所有的点都收到消息(消息可以随边传递) 2.最少需要多少条边才能使得图变成强连通图 对于一个强连通分量,可以当做一个点来考虑,所以我们 ...

  8. 面试题 收集请求k千里马

    收集请求k最大值 个人信息:就读于燕大本科软件project专业 眼下大三; 本人博客:google搜索"cqs_2012"就可以; 个人爱好:酷爱数据结构和算法,希望将来从事算法 ...

  9. SQL Server :理解IAM 页

    原文:SQL Server :理解IAM 页 在以前的文章里,我们讨论了数据页,GAM和SGAM,还有PFS页.今天我们一起来讨论下索引分配映射(Index Allocation Map:IAM)页. ...

  10. 再见,CSDN

    这是第三次的博客, 首先是从百度改变自己 从他的变化二CSDN 看看多年的积累, 真的不想,但CSDN搜电缆和编辑(新MarkDown更烂)实在不敢恭维 再见CSDN, 新的博客 http://my. ...