zoukankan      html  css  js  c++  java
  • HDU

    String

    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.

    InputThe 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.OutputFor each test case, output the answer in a single line.Sample Input

    3 3
    abcabcbcaabc

    Sample Output

    2




    13年长春赛的一道题目,涉及的知识点非常多。

    1.枚举出字符串中固定长度的子串,暴力做法O(n^2),这里用到了窗口移动。
    即以第一个循环节中每个字符为起点,依次向后寻找直到末尾,避免了重复查询。
    2.map也可以作为set使用,并且还可记录重复元素的个数。
    3.hash优化。利用字符串映射效率较低,这里可以将字符串转化为数字。
    将字符看作高进制数,使用unsign long long进行存储(利用了其自动取模的效果),
    种子seed设为质数可以使重复的概率降到最低(可忽略不计)。

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    
    string s;
    map<string,int> mp;
    
    int main()
    {
        int m,l,i,j;
        while(~scanf("%d%d",&m,&l)){
            cin>>s;
            int len=s.length();
            if(m==1){
                printf("%d
    ",len-l+1);
                continue;
            }
            ll ans=0;
            for(i=0;i<l;i++){
                int x=0;
                mp.clear();
                for(j=i;j+l-1<len;j+=l){
                    x++;
                    mp[s.substr(j,l)]++;
                    if(x==m){
                        if(mp.size()==m) ans++;
                    }
                    else if(x>m){
                        x--;
                        if(mp[s.substr(j-m*l,l)]==1){
                            mp.erase(s.substr(j-m*l,l));
                        }
                        else{
                            mp[s.substr(j-m*l,l)]--;
                        }
                        if(mp.size()==m) ans++;
                    }
                }
            }
            printf("%I64d
    ",ans);
        }
        return 0;
    }
    优化前(920ms)
    #include<bits/stdc++.h>
    #define MAX 100005
    #define seed 31
    #define get(a,b) haxi[a]-haxi[a+b]*base[b]
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    
    string s;
    map<ull,int> mp;
    ull base[MAX];
    ull haxi[MAX];
    
    void init(int len){
        haxi[len]=0;
        for(int i=len-1;i>=0;i--){
            haxi[i]=haxi[i+1]*seed+s[i]-'a';
        }
    }
    int main()
    {
        int m,l,i,j;
        base[0]=1;
        for(int i=1;i<=100000;i++){
            base[i]=base[i-1]*seed;
        }
        while(~scanf("%d%d",&m,&l)){
            cin>>s;
            int len=s.length();
            if(m==1){
                printf("%d
    ",len-l+1);
                continue;
            }
            init(len);
            ll ans=0;
            for(i=0;i<l;i++){
                int x=0;
                mp.clear();
                for(j=i;j+l-1<len;j+=l){
                    x++;
                    mp[get(j,l)]++;
                    if(x==m){
                        if(mp.size()==m) ans++;
                    }
                    else if(x>m){
                        x--;
                        if(mp[get(j-m*l,l)]==1){
                            mp.erase(get(j-m*l,l));
                        }
                        else{
                            mp[get(j-m*l,l)]--;
                        }
                        if(mp.size()==m) ans++;
                    }
                }
            }
            printf("%I64d
    ",ans);
        }
        return 0;
    }
    优化后(327ms)
     
  • 相关阅读:
    日志类
    sql查询数据并导出问题
    高并发系统设计(十七):【系统架构】微服务化后,系统架构要如何改造?
    高并发系统设计(十五):【消息队列】如何降低消息队列系统中消息的延迟?
    高并发系统设计(十四):【消息队列】如何消息不丢失?并且保证消息仅仅被消费一次?
    高并发系统设计(十三):消息队列的三大作用:削峰填谷、异步处理、模块解耦
    高并发系统设计(十二):【缓存的正确使用姿势】缓存穿透了怎么办?如何最大程度避免缓存穿透
    高并发系统设计(十一):【缓存的正确使用姿势】缓存如何做到高可用?
    ThinkPad X1 Carbon无法识别第二屏幕
    如何设置两个TPLink路由器桥接
  • 原文地址:https://www.cnblogs.com/yzm10/p/9748478.html
Copyright © 2011-2022 走看看