串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. C++——STL中三种顺序容器的简要差别

    C++ STL 提供了3个顺序容器 :vector, deque, list Vector动态数组.支持高速訪问:list双向链表,支持高速插入和删除. vector 中的元素是顺序存放的.所以随机訪 ...

  2. [ACM] HDU 2063 过山车 (二分图,匈牙利算法)

    过山车 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  3. ecshop 调用其他数据库中的商品

    ecshop中修改includes/cls_ecshop.php中第53行 function table($str) { /* if($str=='goods'){ return '`ecshop3' ...

  4. Oracle JDBC版本区别(转)

    oracle\product\11.2.0\dbhome_1\jdbc\lib ojdbc5.jar ojdbc5dms.jar ojdbc5dms_g.jar ojdbc5_g.jar ojdbc6 ...

  5. Java 里把 InputStream 转换成 String 的几种方法

    我们在 Java 中经常会碰到如何把 InputStream 转换成 String 的情形,比如从文件或网络得到一个 InputStream,需要转换成字符串输出或赋给别的变量. 未真正关注这个问题之 ...

  6. Android Application plugin

          在网易云阅读App上看到了插件管理功能,刚好自己也需要以插件的模式来扩展已有的功能,于是研究了一下,下面是一张网易云阅读App提供的插件模式,只需下载相应的插件就扩展了相应的功能,非常方便 ...

  7. 域名注册查询接口(API)的说明

    1.域名查询 接口采用HTTP,POST,GET协议: 调用URL:http://panda.www.net.cn/cgi-bin/check.cgi 参数名称:area_domain 值为标准域名, ...

  8. android的ViewPager和Animation有些使用(二)

    Animation部分 android的animation分scale,rotate,tranlateAnimation,alpha这些类型的 start animation这里有几种方法: < ...

  9. ORACLE 中的 锁 介绍

    ORACLE 中的 锁 介绍 Oracle数据库支持多个用户同时与数据库进行交互,每个用户都可以同时运行自己的事务,从而也需要对并发访问进行控制.Oracle也是用“锁”的机制来防止各个事务之间的相互 ...

  10. 二分图行列匹配---> hdu2119,hdu1498

    hdu2119 题意:给定一个矩形方格,每个格子里面的数字是0或者1,每次操作可以把一整行或列的1变成0,问最少多少次操作能将1全部变为0 一次可以消除某一行或者某一列的1但是可以这么想,最多有多少个 ...