字符串哈希专题比赛解题报告

· · 个人记录

P13867 [SWERC 2020] Unique Activities

做哈希专题赛(OI 赛制)时场切的,难度不高。

前情:

我们将这题当作 OI 赛制来做,不求简单而求稳。

思路:

:::info[题目大意]{open} 输入一个字符串(由大写字母组成),表示 Emily 一天的活动序列。
要找到最短的只出现一次的子串(连续)。
如果有多个相同长度的最短唯一子串,输出第一个出现的即可。 ::: 很明显,这道题可以用哈希将字符串转为线性结构。

那么当我们将字符串转为线性结构后,可以看到数据范围是:

所以我们可以以 \mathcal O(N\log N) 的时间复杂度稳过这道题。注意,这里是稳,可能有更简便的方法但复杂度更高,这里要我们求稳。

题目说要找到**最短**的**只出现一次**的子串(连续)。 而**只出现一次**自然是越长越有可能,以此二分答案就可以用了。 二分答案自然是有 $\operatorname{check}$ 的。这里 $\operatorname{check}$ 的作用就是检查在长度为 $x$,是否存在有**只出现一次**的子串(连续)。这个应该不用讲了,不懂就看一眼代码吧。 ## code: ```cpp #include<bits/stdc++.h> #define int unsigned long long using namespace std; int n; int hs[300005]; int pw[300005]; int hashh(int l,int r){ return hs[r]-hs[l-1]*pw[r-l+1]; } unordered_map<int,int> f; bool check(int x){ f.clear(); for(int i=1;i<=n-x+1;i++){ f[hashh(i,i+x-1)]++; } for(int i=1;i<=n-x+1;i++){ if(f[hashh(i,i+x-1)]==1){ return 1; } } return 0; } signed main(){ string s; cin>>s; n=s.size(); s=" "+s; pw[0]=1; for(int i=1;i<=n;i++){ pw[i]=pw[i-1]*13331; hs[i]=hs[i-1]*13331+s[i]; } int l=1,r=n; while(l<r){ int mid=(l+r)/2; if(check(mid)){ r=mid; } else{ l=mid+1; } } f.clear(); for(int i=1;i<=n-l+1;i++){ f[hashh(i,i+l-1)]++; } for(int i=1;i<=n-l+1;i++){ if(f[hashh(i,i+l-1)]==1){ cout<<s.substr(i,l); return 0; } } return 0; } ``` # P1381 单词背诵 这道哈希题,我用的是二分。 主要是相对于每一个字符串来进行一次哈希,算出每个字符串所对应的哈希值后,存起来。 二分答案,对于长度区间,计算每个区间内所有要背的加起来,提前计算文章中有多少个需要背诵的。值的注意的是,可能出现不是必备单词的单词,需要特判。 ```cpp #include<bits/stdc++.h> #define int unsigned long long using namespace std; int cnt; int gotohash(string x){ int y=0; for(int i=0;i<x.size();i++){ y=y*13331+x[i]; } return y; } int a[10000005]; unordered_map<int,int> f; unordered_map<int,bool> b; int n; int m; bool check(int x){ f.clear(); int num=0; for(int i=1;i<=x;i++){ f[a[i]]++; if(f[a[i]]==1&&b[a[i]]==1) num++; } if(num==cnt) return 1; for(int i=x+1;i<=m;i++){ f[a[i-x]]--; if(f[a[i-x]]==0&&b[a[i-x]]==1) num--; f[a[i]]++; if(f[a[i]]==1&&b[a[i]]==1) num++; if(num==cnt){ return 1; } } return 0; } signed main(){ cin>>n; for(int i=1;i<=n;i++){ string x; cin>>x; b[gotohash(x)]=1; } cin>>m; for(int i=1;i<=m;i++){ string s; cin>>s; a[i]=gotohash(s); if(f[a[i]]==0&&b[a[i]]==1) cnt++; f[a[i]]=1; } cout<<cnt<<"\n"; int l=0,r=m; while(l<r){ int mid=(l+r)/2; if(check(mid)){ r=mid; } else{ l=mid+1; } } cout<<l; return 0; } ``` # P4656 [CEOI 2017] Palindromic Partitions 这道题有一个贪心策略:能切则切。 例如:$[q,w,q,\dots,q,w,q]

我们发现,区间 [1,3] 与区间 [n-2,n] 相对称,但更优的解法如下:

[1,1]→[n,n],[2,2]→[n-1,n-1],[3,3]→[n-2,n-2]

那么,Just do it!

#include<bits/stdc++.h>
#define int unsigned long long
using namespace std;
int n;
int hs[30000005];
int pw[30000005];
int hashh(int l,int r){
    return hs[r]-hs[l-1]*pw[r-l+1];
}
signed main(){
    int T;
    cin>>T;
    while(T--){
        string s;
        cin>>s;
        n=s.size();
        s=" "+s;
        pw[0]=1;
        for(int i=1;i<=n;i++){
            pw[i]=pw[i-1]*13331;
            hs[i]=hs[i-1]*13331+s[i]; 
        }
        int l=1,r=n,ll=1,rr=n,ans=0;
        while(ll<rr){
            if(hashh(l,ll)==hashh(rr,r)){
                rr--;
                ll++;
                r=rr;
                l=ll;
                ans+=2;
            }
            else{
                rr--;
                ll++;
            }
        }
        if(l<=r) ans++;
        cout<<ans<<"\n";
    }
    return 0;
}