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. 九度OJ 1062:分段函数 (基础题)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:3306 解决:1952 题目描述: 编写程序,计算下列分段函数y=f(x)的值. y=-x+2.5; 0<=x<2 y=2-1. ...

  2. 如果这种方式导致程序明显变慢或者引起其他问题,我们要重新思考来通过 goroutines 和 channels 来解决问题

    https://github.com/Unknwon/the-way-to-go_ZH_CN/blob/master/eBook/09.3.md 9.3 锁和 sync 包 在一些复杂的程序中,通常通 ...

  3. MapReduce-PRODUCTION-DEMAND

    [粗暴的HIVE-SQL]select xyz from abc where ty='sdk' and ret_code=0 and data_source_type=1 and dt between ...

  4. 我的Android进阶之旅------>对Java中注释/**@hide*/的初步认识

    今天写一个调节系统背光亮度的时候,参考了Android中的Setting源码,在源码中有这么一段代码: private static final int MAXIMUM_BACKLIGHT = and ...

  5. lambda map filter 用法

    lambda 可以这样认为,lambda作为一个表达式 非常容易理解,在这里lambda简化了函数定义的书写形式.是代码更为简洁,但是使用函数的定义方式更为直观,易理解. #定义函数:普通方式 def ...

  6. CUDA: 共享内存与同步

    CUDA C支持共享内存, 将CUDA C关键字__shared__添加到变量声明中,将使这个变量驻留在共享内存中.对在GPU上启动的每个线程块,CUDA C编译器都将创建该变量的一个副本.线程块中的 ...

  7. python仪表盘

    1.在这里可以看到pyecharts中有定义好的各种图标类. 复制上面代码,会出现“ModuleNotFoundError: No module named 'pyecharts'”. pip ins ...

  8. Java for LeetCode 083 Remove Duplicates from Sorted List

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  9. Gemini.Workflow 双子工作流入门教程三:定义流程:流程节点、迁移条件参数配置

    简介: Gemini.Workflow 双子工作流,是一套功能强大,使用简单的工作流,简称双子流,目前配套集成在Aries框架中. 下面介绍本篇教程:定义流程:流程节点.迁移条件参数配置. 一.普通节 ...

  10. java和js互调 拨打电话

    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...