String

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.

InputThe 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.OutputFor each test case, output the answer in a single line.Sample Input

3 3
abcabcbcaabc

Sample Output

2

13年长春赛的一道题目,涉及的知识点非常多。

1.枚举出字符串中固定长度的子串,暴力做法O(n^2),这里用到了窗口移动。
即以第一个循环节中每个字符为起点,依次向后寻找直到末尾,避免了重复查询。
2.map也可以作为set使用,并且还可记录重复元素的个数。
3.hash优化。利用字符串映射效率较低,这里可以将字符串转化为数字。
将字符看作高进制数,使用unsign long long进行存储(利用了其自动取模的效果),
种子seed设为质数可以使重复的概率降到最低(可忽略不计)。
#include<bits/stdc++.h>
using namespace std;
typedef long long ll; string s;
map<string,int> mp; int main()
{
int m,l,i,j;
while(~scanf("%d%d",&m,&l)){
cin>>s;
int len=s.length();
if(m==){
printf("%d\n",len-l+);
continue;
}
ll ans=;
for(i=;i<l;i++){
int x=;
mp.clear();
for(j=i;j+l-<len;j+=l){
x++;
mp[s.substr(j,l)]++;
if(x==m){
if(mp.size()==m) ans++;
}
else if(x>m){
x--;
if(mp[s.substr(j-m*l,l)]==){
mp.erase(s.substr(j-m*l,l));
}
else{
mp[s.substr(j-m*l,l)]--;
}
if(mp.size()==m) ans++;
}
}
}
printf("%I64d\n",ans);
}
return ;
}

优化前(920ms)

#include<bits/stdc++.h>
#define MAX 100005
#define seed 31
#define get(a,b) haxi[a]-haxi[a+b]*base[b]
using namespace std;
typedef long long ll;
typedef unsigned long long ull; string s;
map<ull,int> mp;
ull base[MAX];
ull haxi[MAX]; void init(int len){
haxi[len]=;
for(int i=len-;i>=;i--){
haxi[i]=haxi[i+]*seed+s[i]-'a';
}
}
int main()
{
int m,l,i,j;
base[]=;
for(int i=;i<=;i++){
base[i]=base[i-]*seed;
}
while(~scanf("%d%d",&m,&l)){
cin>>s;
int len=s.length();
if(m==){
printf("%d\n",len-l+);
continue;
}
init(len);
ll ans=;
for(i=;i<l;i++){
int x=;
mp.clear();
for(j=i;j+l-<len;j+=l){
x++;
mp[get(j,l)]++;
if(x==m){
if(mp.size()==m) ans++;
}
else if(x>m){
x--;
if(mp[get(j-m*l,l)]==){
mp.erase(get(j-m*l,l));
}
else{
mp[get(j-m*l,l)]--;
}
if(mp.size()==m) ans++;
}
}
}
printf("%I64d\n",ans);
}
return ;
}

优化后(327ms)

 

HDU - 4821 String(窗口移动+map去重+hash优化)的更多相关文章

  1. HDU 4821 String (HASH)

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

  2. HDU 4821 String hash

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

  3. HDU 4821 String 字符串hash

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

  4. HDU 4821 String(BKDRHash)

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

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

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

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

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

  7. HDU 1113 Word Amalgamation (map 容器 + string容器)

    http://acm.hdu.edu.cn/showproblem.php?pid=1113 Problem Description In millions of newspapers across ...

  8. HDU 4821 2013长春现场赛hash

    题意: 一个字符串S  问其中有几个子串能满足以下条件: 1.长度为M*L 2.可以被分成M个L长的小串  每个串都不一样 分析: hash方法,一个种子base,打表出nbase[i]表示base的 ...

  9. STL之map应用 +hash表(51nod 1095)

    题目:Anigram单词 题意:给出词典,再给出一些单词,求单词的Anigram数量. 思路:先将字串转换成哈希表,然后再用map链接. hash表构造方法汇总:http://www.cnblogs. ...

随机推荐

  1. mysql批量插入测试数据

    一.建表语句 use test; create table student( Sno ) NOT NULL COMMENT '学号', Sname ) NOT NULL COMMENT '姓名', S ...

  2. 【题解】P4799[CEOI2015 Day2]世界冰球锦标赛

    [题解][P4799 CEOI2015 Day2]世界冰球锦标赛 发现买票顺序和答案无关,又发现\(n\le40\),又发现从后面往前面买可以通过\(M\)来和从前面往后面买的方案进行联系.可以知道是 ...

  3. python splinter chromedriver下载地址(国内可用)

    http://chromedriver.storage.googleapis.com/index.html

  4. python获取当前的时间

    打印出当前的年月日时分秒 print(time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))) 2018-09-05 09:39: ...

  5. 【栈】日志分析(BSOJ2981)

    Description M海运公司最近要对旗下仓库的货物进出情况进行统计.目前他们所拥有的唯一记录就是一个记录集装箱进出情况的日志.该日志记录了两类操作:第一类操作为集装箱入库操作,以及该次入库的集装 ...

  6. qemu仿真执行uboot和barebox

    先安装qemu: apt-get install qemu-system 交叉编译器可以选择友善之臂:http://arm9download.cncncn.com/mini2440/linux/arm ...

  7. UVA - 11475 Extend to Palindrome —— 字符串哈希 or KMP or 后缀数组

    题目链接:https://vjudge.net/problem/UVA-11475 题意: 给出一个字符串,问在该字符串后面至少添加几个字符,使得其成为回文串,并输出该回文串. 题解: 实际上是求该字 ...

  8. Linux tomcat安装详解

    一.tomcat安装 1.下载JDK和Tomcat //通过wget下载 wget http://mirrors.tuna.tsinghua.edu.cn/apache/tomcat/tomcat-8 ...

  9. 创建blog APP

    声明:此Django分类下的教程是追梦人物所有,地址http://www.jianshu.com/u/f0c09f959299,本人写在此只是为了巩固复习使用 什么是APP呢,Django里的APP其 ...

  10. HDU 1005 Number Sequence:矩阵快速幂

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1005 题意: 数列{f(n)}: f(1) = 1, f(2) = 1, f(n) = ( A*f(n ...