String

Time Limit: 1 Sec

Memory Limit: 256 MB

题目连接

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=95149#problem/I

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

HINT

题意

给你一个字符串,让你找到有多少个长度为m*l的子串,由m个长度为l的不同的串构成的

题解:

hash一下之后,就直接暴力找就好了

暴力得用类似滑块一样优化一下就好了

代码:

#include<stdio.h>
#include<iostream>
#include<cstring>
#include<map>
using namespace std;
typedef long long ll;
#define maxn 100005
ll h[maxn*];
ll h2[maxn*];
char str[maxn*];
int n,len,k,s1,s3,vis[maxn],sum[maxn];
ll N=;
ll p=;
ll powp[maxn*]; void get_hash()
{
h[]=(ll)str[];
for(int i=;i<n;i++)
{
h[i]=(h[i-]*p+(ll)str[i]);
while(h[i]<)
h[i]+=N;
if(h[i]>=N)
h[i]%=N;
}
powp[]=1LL;
for(int i=;i<n;i++)
{
powp[i]=powp[i-]*p;
while(powp[i]<)
powp[i]+=N;
if(powp[i]>=N)
powp[i]%=N;
}
}
ll gethash(int l,int r)
{
if(!l)
return h[r];
ll ans=h[r]-h[l-]*powp[r-l+];
if(ans<)
ans%=N;
if(ans<)
ans+=N;
if(ans>=N)
ans%=N;
return ans;
} map<ll ,int> H;
int main()
{
int M,L;
while(scanf("%d%d",&M,&L)!=EOF)
{
memset(vis,,sizeof(vis));
memset(sum,,sizeof(sum));
scanf("%s",str);
len = strlen(str);
n = len;
get_hash();
/*
int ans = len-M*L+1;
for(int i=0;i<L;i++)
{
H.clear();
int flag=0;
for(int j=0;i+(j+1)*L<=len;j++)
{
ll pp=gethash(i+j*L,i+(j+1)*L-1);
if(H[pp])
{
vis[i+(H[pp]-1)*L]=1;
vis[i+j*L]=1;
}
H[pp]=j+1;
}
if(vis[i])sum[i]=1;else sum[i]=0;
for(int j=1;i+j*L+L<=len;j++)
{
sum[i+j*L]=sum[i+(j-1)*L];
if(vis[i+j*L])
{
sum[i+j*L]++; }
if(i+M*L<=len&&sum[i+(M-1)*L]!=0) ans--;
for(int j=M;i+j*L+L<=len;j++)
if(sum[i+(j-M)*L]!=sum[i+j*L]) ans--;
}
*/
int ans = ;
for(int i=;i<L;i++)
{
H.clear();
for(int j=;j<M&&i+(j+)*L-<len;j++)
{
//cout<<i+j*L<<" "<<i+(j+1)*L-1<<" 1"<<" "<<gethash(i+j*L,i+(j+1)*L-1)<<endl;
H[gethash(i+j*L,i+(j+)*L-)]++;
}
//cout<<H.size()<<endl;
if(H.size()==M)ans++;
for(int j=M;i+(j+)*L-<len;j++)
{
//cout<<i+j*L<<" "<<i+(j+1)*L-1<<" 2"<<" "<<gethash(i+(j-M)*L,i+(j+1-M)*L-1)<<endl;
H[gethash(i+(j-M)*L,i+(j+-M)*L-)]--;
if(H[gethash(i+(j-M)*L,i+(j+-M)*L-)]==)
H.erase(gethash(i+(j-M)*L,i+(j+-M)*L-));
H[gethash(i+j*L,i+(j+)*L-)]++;
//cout<<H.size()<<endl;
if(H.size()==M)ans++;
}
}
printf("%d\n",ans);
}
}

HDU 4821 String hash的更多相关文章

  1. hdu 4821 字符串hash+map判重 String (长春市赛区I题)

    http://acm.hdu.edu.cn/showproblem.php?pid=4821 昨晚卡了非常久,開始TLE,然后优化了之后,由于几个地方变量写混.一直狂WA.搞得我昨晚都失眠了,,. 这 ...

  2. HDU 4821 String (HASH)

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

  3. 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 ...

  4. HDU 4821 String 字符串hash

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

  5. 2013 Asia Regional Changchun I 题,HDU(4821),Hash

    题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=4821 解题报告:搞了很久,总算搞出来了,还是参考了一下网上的解法,的确很巧,和上次湘潭的比 ...

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

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

  7. HDU 4821 String(BKDRHash)

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

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

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

  9. HDU 4821 字符串hash

    题目大意: 希望找到连续的长为m*l的子串,使得m个l长的子串每一个都不一样,问能找到多少个这样的子串 简单的字符串hash,提前预处理出每一个长度为l的字符串的hash值 #include < ...

随机推荐

  1. 11g 重建EM 报ORA-20001: SYSMAN already exists

    今天在安装11g(11.1.0.7.0)数据库之后,通过emca -config dbcontrol db -repos create 命令手工创建em的时候报错,查看日志后发现有以下错误 CONFI ...

  2. 改变DEV控件的字体 z

    改变所有的组件字体,使用AppearanceObject.DefaultFont属性:static void Main() {    DevExpress.Utils.AppearanceObject ...

  3. delegate 为什么用 weak属性

    weak指针主要用于“父-子”关系,父亲拥有一个儿子的strong指针,因此是儿子的所有者:但是为了阻止所有权回环,儿子需要使用weak指针指向父亲:你的viewcontroller通过strong指 ...

  4. Apache benchmark对网站进行压力测试

    Apache Benchmark下载:http://down.tech.sina.com.cn/page/3132.html ab 的全称是 ApacheBench , 是 Apache 附带的一个小 ...

  5. [Web API] 如何让 Web API 统一回传格式以及例外处理[转]

    [Web API] 如何让 Web API 统一回传格式以及例外处理 前言 当我们在开发 Web API 时,一般的情况下每个 API 回传的数据型态或格式都不尽相同,如果你的项目从头到尾都是由你一个 ...

  6. angular form-data文件上传

    前言:很久没更新博客,最近公司pc端技术选型用angular,这几天就赶鸭子上架,硬着头皮直接上手angular.其中有许多小坑陆陆续续踩起走.今天就遇到一个比较常见的问题:图片上传. 主题:图片上传 ...

  7. EasyMock

    使用 EasyMock 更轻松地进行测试 窥探EasyMock(1)基础使用篇 窥探EasyMock(2)进阶使用篇

  8. tensorflow-cnn

    需要安装 python,numpy,tensorflow,运行代码即可. tensorflow很好装,用pip安装即可. 可以参照http://wiki.jikexueyuan.com/project ...

  9. Cocos2dx游戏源码合集(BY懒骨头+持续更新+2014.02.21)

    转自:http://blog.csdn.net/iamlazybone/article/details/19612941 声明: <萝莉快跑><喵汪大战>两个demo的原作者b ...

  10. 【转】并发编程之GCD

    http://blog.xcodev.com/blog/2013/11/04/gcd-intro/ Dispatch Queue Dispatch Queue是一个任务执行队列,可以让你异步或同步地执 ...