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 1007:奥运排序问题 (排序)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:7344 解决:1568 题目描述: 按要求,给国家进行排名. 输入: 有多组数据. 第一行给出国家数N,要求排名的国家数M,国家号从0到N- ...

  2. Android数据格式化

    1.文件大小格式化: Log.d(TAG, Formatter.formatFileSize(this, 100)); //100 B Log.d(TAG, Formatter.formatFileS ...

  3. 辛星跟您玩转vim第四节之操作文本内容

    首先值得一提的是.我的vim教程pdf版本号已经写完了.大家能够去下载,这里是csdn的下载地址:csdn下载,假设左边的下载地址挂掉了,也能够自行在浏览器以下输入例如以下地址进行下载:http:// ...

  4. Yii 框架 URL路径简化

    Yii 框架的訪问地址若不简化会让人认为非常繁琐.未简化的地址一般格式例如以下: http://localhost:80/test/index.php?r=xxx/xxx/xxx 若是带有參数会更复杂 ...

  5. RDS for MySQL 删除数据后空间没有减少处理方法

    公司的程序和数据库部署在阿里云上,数据库使用的是阿里云的RDS,这天,经理在开发群中发了一个信息: 您的RDS实例rm********0oq的磁盘在过去一周平均使用率已超过80.%,建议您对实例规格进 ...

  6. (转)source insight 窗口嵌入

    昨天用了一下source insight ,都说很强大,也有感觉,但是这个强大的东西往往不是那么容易弄清楚的,或者一下子就好上手的,工具强大,功能复杂多样,一开始不知道怎么入手,以后慢慢来吧,学习是要 ...

  7. jQuery 的文档操作

    在 js 中也有DOM操作,也可以进行 增删改查 ,但是通过 js 的DOM操作会发现,我的天哪,代码好多.但是 jQuery的文档操作就少了很多. js 中 DOM 的示例 : var box = ...

  8. linux系统调用mount全过程分析【转】

    本文转载自:https://blog.csdn.net/skyflying2012/article/details/9748133 系统调用本身是软中断,使用系统调用,内核也陷入内核态,异常处理,找到 ...

  9. Contiki Process概述

    本文涉及到的Protothread机制知识,在http://www.cnblogs.com/songdechiu/p/5793717.html 一.进程类型 进程类型主要有协同式(cooperativ ...

  10. zabbix增加服务器tcp监控

     zabbix server web界面,需要导入 tcp 监控模板 操作步骤:     Configuration --> Templates --> Import ,选择 本地的 zb ...