zoukankan      html  css  js  c++  java
  • 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.
     
    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
     

    题意:

      给你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
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    javascript 对象属性的添加,删除,json对象和字符串转换方法等
    利用jquery.form.js实现将form提交转为ajax方式提交的方法(带上传的表单提交)
    js封装的一行半显示省略号。(字数自由控制)
    jq和js中click 事件的几种方式总结和click事件的累加问题解决办法
    jquery $(document).ready() 与js原生的window.onload的区别总结
    jquery中attr和prop的区别
    织梦文档按权重排序
    织梦channelartlist标签内使用currentstyle
    dedecms无法下载远程jpeg图片 织梦不能提取文章内容中的jpeg图片生成缩略图
    织梦dedecms去除友情链接中的li和span
  • 原文地址:https://www.cnblogs.com/zxhl/p/7226544.html
Copyright © 2011-2022 走看看