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. 【Java】Java处理double相加的结果异常

    方式一(四舍五入):保留两位小数 double f = 111231.5585; BigDecimal b = new BigDecimal(f); double f1 = b.setScale(2, ...

  2. 【转】linux : waitpid函数

    原文网址:http://blog.csdn.net/jifengszf/article/details/3067841 [waitpid系统调用]       功能描述: 等待进程改变其状态.所有下面 ...

  3. HDU 5762 Teacher Bo

    Teacher Bo Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Others)Tota ...

  4. <译>Selenium Python Bindings 5 - Waits

    如今,大多数的Web应用程序使用AJAX技术.当页面加载到浏览器,页面中的元素也许在不同的时间间隔内加载.这使得元素很难定位,如果在DOM中的元素没有呈现,它将抛出ElementNotVisibleE ...

  5. 学习笔记之Linux内核编译过程

    准备工作 物理主机:win8(32位) 虚拟机工具:VirtualBox_4.3.16_Win32 虚拟主机:xubuntu-12.04.4 安装virtualBox功能增强包 设置好虚拟机与主机的共 ...

  6. 从python的yield说起

    前段时间在读trac 中wiki模块的源码的时候,发现了很多地方都使用了yiled这一关键词, 感觉是在需要返回某个值的地方通过yield来代替return, 不是很明白其用法,所以仔细研究下. 一个 ...

  7. ACCESS TOKEN

    Access Token 在微信公众平台接口开发中,Access Token占据了一个很重要的地位,相当于进入各种接口的钥匙,拿到这个钥匙才有调用其他各种特殊接口的权限. access_token是公 ...

  8. kendoui-grid篇

    kendo确实是个好东西,能够让我们专注于后端开发,无需在效果呈现上花大力气,唯一的缺点,它是收费的,但是我目前还没发现为嘛要掏钱,因为free的也满足了我的需求. kendoUI For asp.m ...

  9. java 复习003

    今天主要复习下数据结构的东西 树 自平衡二叉查找树 AVL树(高平衡树)(wiki) 特性:任何节点的两个子树的高度最大差别为一 时间复杂度:查找.插入和删除在平均和最坏情况下都是O(log n) 红 ...

  10. Android JNI之JAVA调用C/C++层

    转载请声明:原文转自:http://www.cnblogs.com/xiezie/p/5929996.html 一.java调用本地函数的开发步骤: 1.编写本地方法的类(可以说是用来叙述本地方法的类 ...