zoukankan      html  css  js  c++  java
  • 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);
        }
    }
  • 相关阅读:
    [LeetCode] Max Increase to Keep City Skyline 保持城市天际线的最大增高
    [LeetCode] Bricks Falling When Hit 碰撞时砖头掉落
    [LeetCode] Number of Lines To Write String 写字符串需要的行数
    [LeetCode] Unique Morse Code Words 独特的摩斯码单词
    [LeetCode] Find Eventual Safe States 找到最终的安全状态
    [LeetCode] Minimum Swaps To Make Sequences Increasing 使得序列递增的最小交换
    [LeetCode] Similar RGB Color 相似的红绿蓝颜色
    [LeetCode] Champagne Tower 香槟塔
    [LeetCode] Smallest Rotation with Highest Score 得到最高分的最小旋转
    [LeetCode] All Paths From Source to Target 从起点到目标点到所有路径
  • 原文地址:https://www.cnblogs.com/qscqesze/p/4877955.html
Copyright © 2011-2022 走看看