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;
    }
  • 相关阅读:
    linux日常。
    tp5中的config类和config助手函数
    TP5隐藏index.php
    TP5读取数据概述
    TP5的安装部署概要
    eclipse4.7中文包安装方法。
    利用mysqldump备份magento数据库
    MySQL 基础知识
    PHP 基础知识
    妖怪与和尚过河问题
  • 原文地址:https://www.cnblogs.com/zxhl/p/7226544.html
Copyright © 2011-2022 走看看