HDU - 4821 String(窗口移动+map去重+hash优化)

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\n",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\n",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\n",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\n",ans);
    }
    return 0;
}
优化后(327ms)
 
posted @ 2018-10-06 21:06  yzm10  阅读(355)  评论(0编辑  收藏  举报