• HDU 4821 String (HASH)


    题意:给你一串字符串s,再给你两个数字m l,问你s中可以分出多少个长度为m*l的子串,并且子串分成m个长度为l的串每个都不完全相同

    首先使用BKDRHash方法把每个长度为l的子串预处理成一个数字,接着根据题意直接map判重

    BKDRHash:一种常用字符串hash,hash简单来说就是把一串字符串通过一些转化成为一个数字,并保证相同字符串转化的数字一样,不相同字符串转化的数字一定不一样。方法就是hash[i]=hash[i-1]*seed(进制)+str[i]-'a'+1(注意要加一,因为不能为0)

    注意这儿unsigned long long可以自动取模,还有BKDRHash需要取进制31,131这些。

    #include<set>
    #include<map>
    #include<queue>
    #include<stack>
    #include<cmath>
    #include<vector>
    #include<string>
    #include<cstdio>
    #include<cstring>
    #include<stdlib.h>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    #define eps 1E-8
    /*注意可能会有输出-0.000*/
    #define Sgn(x) (x<-eps? -1 :x<eps? 0:1)//x为两个浮点数差的比较,注意返回整型
    #define Cvs(x) (x > 0.0 ? x+eps : x-eps)//浮点数转化
    #define zero(x) (((x)>0?(x):-(x))<eps)//判断是否等于0
    #define mul(a,b) (a<<b)
    #define dir(a,b) (a>>b)
    typedef long long ll;
    typedef unsigned long long ull;
    const int Inf=1<<28;
    const double Pi=acos(-1.0);
    const int Mod=1e9+7;
    const int Max=100010;
    const ull seed=31;//31 131 1313 ......
    char str[Max];
    ull hhash[Max],base[Max];//ull自动取模
    map<ull,int> mp;//判重
    void Init()//初始化seed倍数,用于hash删前一个字符
    {
        base[0]=1ull;
        for(int i=1; i<Max; ++i)
            base[i]=base[i-1]*seed;
        return;
    }
    void Hash(int l,int len)//把每l个字符压缩成为一个数字
    {
        for(int i=0; i<len; ++i)
        {
            if(i>=l)//首先删除前一个字符
                hhash[i]=hhash[i-1]-base[l-1]*(str[i-l]-'a'+1);
            else if(i)
                hhash[i]=hhash[i-1];
            else
                hhash[0]=0ull;
            hhash[i]=hhash[i]*seed+str[i]-'a'+1;//hash
            //printf("%llu
    ",hhash[i]);
        }
        return ;
    }
    int Solve(int m,int l,int len)
    {
        int ans=0,i;
        int now,sum;//记录当前运行到第几个 记录总共有多少种值
        Hash(l,len);//存下每l位map判重
        for(int i=l-1;i<l+l-1;++i)
        {
            now=sum=0;
            mp.clear();
            for(int j=i;j<len;j+=l)
            {
                now++;
                if(now>m)//删除前面超出区间的值
                {
                    mp[hhash[j-m*l]]--;
                 if(mp[hhash[j-m*l]]==0)
                    sum--;
                }
                if(!mp.count(hhash[j])||mp[hhash[j]]==0)//此值在此子区间没有
                {
                    mp[hhash[j]]=1;
                    sum++;
                }
                else
                    mp[hhash[j]]++;
                if(sum==m)
                    ans++;
            }
        }
        return ans;
    }
    int main()
    {
        int m,l;
        Init();//初始化
        while(~scanf("%d %d",&m,&l))
        {
            scanf("%s",str);
            printf("%d
    ",Solve(m,l,strlen(str)));
        }
        return 0;
    }
  • 相关阅读:
    Objective-C学习笔记2013(Category 扩展(类目)/分类)(4)
    Objective-C学习笔记(static,self,super)
    Objective-C学习笔记2013(3)[NSArray]数组[在可变数组中,加用add减用remove]
    Objective-C学习笔记2013(2)[NSNumber]
    C语言II博客作业04
    C语言II博客作业03
    C语言II博客作业02
    C语言II博客作业01
    C语言学期总结
    第一次作业
  • 原文地址:https://www.cnblogs.com/zhuanzhuruyi/p/5887331.html
Copyright © 2020-2023  润新知