• HDU 4821 String hash


    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*4];
    ll h2[maxn*4];
    char str[maxn*4];
    int n,len,k,s1,s3,vis[maxn],sum[maxn];
    ll N=1000000007;
    ll p=31;
    ll powp[maxn*4];
    
    void get_hash()
    {
        h[0]=(ll)str[0];
        for(int i=1;i<n;i++)
        {
            h[i]=(h[i-1]*p+(ll)str[i]);
            while(h[i]<0)
                h[i]+=N;
            if(h[i]>=N)
                h[i]%=N;
        }
        powp[0]=1LL;
        for(int i=1;i<n;i++)
        {
            powp[i]=powp[i-1]*p;
            while(powp[i]<0)
                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-1]*powp[r-l+1];
        if(ans<0)
            ans%=N;
        if(ans<0)
            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,0,sizeof(vis));
            memset(sum,0,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 = 0;
            for(int i=0;i<L;i++)
            {
                H.clear();
                for(int j=0;j<M&&i+(j+1)*L-1<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+1)*L-1)]++;
                }
               //cout<<H.size()<<endl;
                if(H.size()==M)ans++;
                for(int j=M;i+(j+1)*L-1<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+1-M)*L-1)]--;
                    if(H[gethash(i+(j-M)*L,i+(j+1-M)*L-1)]==0)
                        H.erase(gethash(i+(j-M)*L,i+(j+1-M)*L-1));
                    H[gethash(i+j*L,i+(j+1)*L-1)]++;
                    //cout<<H.size()<<endl;
                    if(H.size()==M)ans++;
                }
            }
            printf("%d
    ",ans);
        }
    }
  • 相关阅读:
    leetcode122
    leetcode121
    leetcode773
    leetcode803
    leetcode658
    leetcode723
    leetcode134
    leetcode340
    leetcode721
    leetcode362
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4877955.html
Copyright © 2020-2023  润新知