HDU 4821 String 字符串hash
Posted Meek
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 4821 String 字符串hash相关的知识,希望对你有一定的参考价值。
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.
(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.
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+10, MM = 1e3+20,inf = 2e9; const LL mod = 10000019ULL; inline LL read() { LL x=0,f=1;char ch=getchar(); while(ch<‘0‘||ch>‘9‘){if(ch==‘-‘)f=-1;ch=getchar();} while(ch>=‘0‘&&ch<=‘9‘){x=x*10+ch-‘0‘;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[0] = 1; for(int i = 1; i < N; ++i) sqr[i] = sqr[i-1] * mod; while(scanf("%d%d",&M,&L)!=EOF) { scanf("%s",sa+1); int n = strlen(sa+1); has[0] = 0; for(int i = 1; i <= n; ++i) { has[i] = has[i-1] * mod + sa[i] - ‘a‘ + 1; } int ans = 0; for(int i = 1; i <= L && i + M * L - 1 <= n; ++i) { int cnt = 0; s.clear(); int ll = 1,rr = 0; for(int j = i; j + L - 1 <= n; j += L) { int l = j, r = j + L - 1; ULL now = has[r] - has[l-1]*sqr[L]; if(s[now] == 0){ bhas[++rr] = now; if(rr - ll + 1 >= M)ans+=1; s[now] = 1; continue; } else { while(ll <= rr && bhas[ll]!=now) { s[bhas[ll++]] = 0; } s[bhas[ll++]] = 0; bhas[++rr] = now; if(rr - ll + 1 >= M)ans+=1; s[now] = 1; } } } printf("%d\n",ans); } return 0; }
以上是关于HDU 4821 String 字符串hash的主要内容,如果未能解决你的问题,请参考以下文章
hdu 4821 字符串hash+map判重 String (长春市赛区I题)