HDU 4821 String(Hash)

原题链接

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.

Limits
T≤500000
−100≤x,y≤100
1≤r≤100

Output

For each test case, output the answer in a single line.t)

Sample Input

3 3
abcabcbcaabc

Sample Output

2

题目大意

每组给出m,l和一个字符串。现在在这个字符串中找出连续m段长度为l的子串,使得这m个子串各不相同,问一共有多少种取法。

解题思路

由于题目要求m个字符串各不相同,所以考虑为每个字符串取哈希值,然后用map取储存, 这样就可以根据map的大小去快速判断是否有重复。

AC代码

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cctype>
#include<algorithm>
#include<cmath>
#include<vector>
#include<string>
#include<queue>
#include<list>
#include<stack>
#include<set>
#include<map>
#define ll long long
#define ull unsigned long long
#define db double
//#define rep(i,n) for(int i = 0;i < n; i++)
//#define rep(i,n) for(int i = 0;i < n; i++)
#define rep(i,a,b) for (int i=(a),_ed=(b);i<=_ed;i++)
//#define rep(i,a,b) for(int i=(a);i<(b);++i)
#define fil(a,b) memset((a),(b),sizeof(a))
#define cl(a) fil(a,0)
#define pb push_back
#define mp make_pair
#define exp 2.7182818
#define PI 3.141592653589793238462643383279502884
#define inf 0x3f3f3f3f
#define fi first
#define se second
#define eps 1e-6
#define MOD 1000000007ll
using namespace std;

const int maxn=100010;
ull xp[maxn];
ull hasha[maxn];
void init()
{
    for(int i=1;i<maxn;++i)
    {
        xp[i]=xp[i-1]*175;
    }
}
ull calhash(int i,int l)
{
    return hasha[i]-hasha[i+l]*xp[l];
}
void gethash(char* str)
{
    int len=strlen(str);
    for(int i=len-1;i>=0;--i)
    {
        hasha[i]=hasha[i+1]*175+(str[i]-'a'+1);
    }
}
char in[maxn+10];
int main(void) 
{
    //freopen("input.txt","r",stdin);
    //freopen("output.txt","w",stdout);
    ll m,l;
    while(scanf("%I64d%I64d",&m,&l)!=EOF)
    {
        scanf("%s",in);
        xp[0]=1;
        init();
        gethash(in);
        int leng=strlen(in);
        if(m*l>leng) printf("0\n");
        int res=0;
        for(int i=0;i<=l-1;++i)
        {
            map<ull,int> mmp;
            int sti=0;
            int head,tail;
            for(int j=1;i+(j-1)*l+l-1<leng;++j)
            {
                if(j<m)
                {
                    mmp[calhash(i+(j-1)*l,l)]++;
                }   
                else if(j==m)
                {
                    mmp[calhash(i+(j-1)*l,l)]++;
                    head=j;
                    tail=1;
                    if(mmp.size()==m) res++;
                }
                else
                {
                    head++;
                    mmp[calhash(i+(j-1)*l,l)]++;
                    mmp[calhash(i+(tail-1)*l,l)]--;
                    if(mmp[calhash(i+(tail-1)*l,l)]==0) mmp.erase(calhash(i+(tail-1)*l,l));
                    tail++;
                    if(mmp.size()==m) res++;
                }
            }

        }
        printf("%d\n",res);
    }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/xj949967574/article/details/77451270