String

Problem 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
 

题意:

  给你M和L,和一个字符串S。

  要求找出S的子串中长度为L*M,并且可以分成M段,每段长L,并且M段都不相同的子串个数。

题解:

  枚举起点

  hash每个前缀串

  那么一段子串的hash值就可以快速求出

  twopointsO(n)求出长度M*L,,M个连续串是否相同

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<cmath>
#include<map>
using namespace std;
#pragma comment(linker, "/STACK:102400000,102400000")
#define ls i<<1
#define rs ls | 1
#define mid ((ll+rr)>>1)
#define pii pair<int,int>
#define MP make_pair
typedef long long LL;
typedef unsigned long long ULL;
const long long INF = 1e18+1LL;
const double pi = acos(-1.0);
const int N = 5e5+, MM = 1e3+,inf = 2e9; const LL mod = 10000019ULL;
inline LL read()
{
LL x=,f=;char ch=getchar();
while(ch<''||ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>=''&&ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
} map<ULL,int > s;
ULL bhas[N],has[N],sqr[N];
int M,L;
char sa[N];
int vis[N];
int main() {
sqr[] = ;
for(int i = ; i < N; ++i) sqr[i] = sqr[i-] * mod;
while(scanf("%d%d",&M,&L)!=EOF) {
scanf("%s",sa+);
int n = strlen(sa+);
has[] = ;
for(int i = ; i <= n; ++i) {
has[i] = has[i-] * mod + sa[i] - 'a' + ;
}
int ans = ;
for(int i = ; i <= L && i + M * L - <= n; ++i) {
int cnt = ;
s.clear();
int ll = ,rr = ;
for(int j = i; j + L - <= n; j += L) {
int l = j, r = j + L - ;
ULL now = has[r] - has[l-]*sqr[L];
if(s[now] == ){
bhas[++rr] = now;
if(rr - ll + >= M)ans+=;
s[now] = ;
continue;
}
else {
while(ll <= rr && bhas[ll]!=now) {
s[bhas[ll++]] = ;
}
s[bhas[ll++]] = ;
bhas[++rr] = now;
if(rr - ll + >= M)ans+=;
s[now] = ;
}
}
}
printf("%d\n",ans);
}
return ;
}

HDU 4821 String 字符串hash的更多相关文章

  1. HDU 4821 String (HASH)

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

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

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

  3. Hdu 4821 (字符串hash+map)

    题目链接https://vjudge.net/problem/HDU-4821 题意:给定字符串S ,询问用几个子串满足 : 1.长度为n*len  . 2. n个子串都不相同. 题解:倒序hash将 ...

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

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

  5. hdu 4622 Reincarnation 字符串hash 模板题

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4622 题意:给定一个长度不超过2000的字符串,之后有不超过1e5次的区间查询,输出每次查询区间中不同 ...

  6. HDU 4821 String hash

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

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

  8. HDU 4821 String(BKDRHash)

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

  9. 魔咒词典 HDU - 1880 (字符串hash 单hash转int或者 双hash )

    哈利波特在魔法学校的必修课之一就是学习魔咒.据说魔法世界有100000种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一个需要的魔咒,所以他需要你的帮助. 给你一部魔咒词 ...

随机推荐

  1. hdu_2070_Fibbonacci Number

    这个题我用long long ,printf("%lld");输出就错误了 我用__int64,printf("%I64d");输出就正确了 这点需要注意. # ...

  2. SPOJ-New Distinct Substrings,注意会爆int

    SUBST1 - New Distinct Substrings 和上一题题意一样,只是数据范围有所改动,50000. 思路还是和上一题一样,所有字串数(len+1)*len/2.注意这里可能爆int ...

  3. EXPDP/IMPDP任务的查看与管理

    EXPDP/IMPDP相比传统的exp/imp的最本质区别在于服务器端执行,客户端发出指定后,通过API启动服务器的备份job,在执行过程中,可以拿下Ctrl+C组合键,退出当前交互模式,退出之后,导 ...

  4. [TyvjP1519] 博彩游戏(AC自动机 + DP)

    传送门 和bzoj1030一个德性 #include <queue> #include <cstdio> #include <cstring> #define N ...

  5. 算法复习——凸包加旋转卡壳(poj2187)

    题目: Description Bessie, Farmer John's prize cow, has just won first place in a bovine beauty contest ...

  6. SSD ECC中的LDPC编解码原理

    转自:http://blog.csdn.net/zhuzongpeng/article/details/78899198 目前SSD中ECC纠错代码主要两种BCH和LDPC.不过,随着SSD对ECC纠 ...

  7. MarsEdit 快速插入代码

    <div class="cnblogs_Highlighter"> <pre class="brush:objc;gutter:true;"& ...

  8. android获取手机号

    private String getPhoneNum(){ //与手机建立连接 TelephonyManager tm = (TelephonyManager)getSystemService(Con ...

  9. sql-trace-10046-trcsess-and-tkprof

    https://oracle-base.com/articles/misc/sql-trace-10046-trcsess-and-tkprof awr/statspack,sqlprofile,sq ...

  10. Java生成读取条形码和二维码图片

    原文:http://www.open-open.com/code/view/1453520722495 package zxing; import com.google.zxing.BarcodeFo ...