跳到正文

题解

P13718 [GCPC 2024] Copycat Catcher

Leaf_c17约 10 分钟
原文
文章信息与版本

文章信息

发布
2026年7月26日
收录
2026年9月27日
更新
2026年11月13日
篇幅
约 10 分钟
获赞
1
题目
P13718

版本

  1. v1当前
关于AI

本文使用 AI 进行润色,因为作者语言表达能力有限,思路、代码均为作者本人所写。

大体思路

序列由常量单词、单字母变量构成。 两个序列模式等价充要条件:

  1. 相同位置的常量单词完全一致;
  2. 变量允许统一重命名,变量之间相等关系保持不变。

示例 [a, hello, a] ≡ [z, hello, z] [a, b, a] ≡ [x, y, x] [a, hello, b] ≠ [a, hello, a]

标准化方案

将序列转为范式序列:

  • 变量:按首次出现顺序分配编号 1,2,3...;重复出现沿用原有编号;
  • 常量:分配偏移后的唯一 ID(如 53 + 常量编号)。

结论:范式完全相同 ⇔ 两个序列模式等价。

初始方案:朴素暴力版(不优化,TLE)

枚举起点 ll,右端点 rr 向外扩展;

对每个子串 [l,r][l,r],每次从头遍历 l−>rl->r 重建范式序列,再求哈希保存。

复杂度:

sum_l=0n−1sum_r=ln−1(r−l+1)=O(n3)sum\_{l=0}^{n-1}\\sum\_{r=l}^{n-1}(r-l+1) = O(n^3)\\ n=2000n=2000 时运算量达到 8∗10108*10^{10} ,TLE。

优化:同起点增量构造范式(降至 O(n2)O(n^2))

固定起点 l,右端 r 从小到大逐步扩展:

  1. 清空变量映射数组、新建空范式数组 nor;
  2. r 从 l 遍历至末尾:
    • 取出当前元素;
    • 变量:查询映射,首次出现分配新编号;常量查询全局字典映射;
    • 将编号追加到 nor;
    • nor 恰好为子串 \([l,r]\) 的范式序列,计算哈希存入分组容器。

复杂度:每个元素只会被加入范式数组一次,总操作 O(n2)O(n^2)。 n=2000n=2000,总子串数量约 4∗1064*10^6,不会超时。

AC Code:

C++
#include<bits/stdc++.h>
#define ll long long
using namespace std;
/*====================*/
const int N=1e2+10;
/*====================*/
const int MOD=1e9+7;
/*====================*/
class C_Hash{
private:
    static constexpr int HP=998244353;
    vector<ll>pow,invbase,sumhash;
public:
    void Init(const vector<ll>&s,ll base=131,ll inv=922042494){
        ll n=s.size();
        pow.assign(n,0);
        invbase.assign(n,0);
        sumhash.assign(n,0);
        for (int i=0;i<n;i++){
            if(i==0)pow[i]=1;
            else pow[i]=pow[i-1]*base%HP;
        }
        for(int i=0;i<n;i++){
            if(i==0)invbase[i]=1;
            else invbase[i]=invbase[i-1]*inv%HP;
        }
        for(int i=0;i<n;i++){
            ll cc=s[i]*pow[i]%HP;
            if(i==0)sumhash[i]=cc;
            else sumhash[i]=(sumhash[i-1]+cc)%HP;
        }
    }
    ll operator()(ll l,ll r)const{
        ll res=sumhash[r];
        if(l){
            res-=sumhash[l-1];
            res+=HP;
        }
        return res*invbase[l]%HP;
    }
};
using ull = unsigned long long;
class C_DoubleHash{
private:
    C_Hash hash1;
    C_Hash hash2;
public:
    void Init(const vector<ll>&s){
        hash1.Init(s,131,922042494);
        hash2.Init(s,233,844009174);
    }
    ull operator()(ll l,ll r)const{
        ull h1 = hash1(l,r);
        ull h2 = hash2(l,r);
        return (h1 << 32) | h2;
    }
};
/*====================*/
bool iv(const string& str){
    if(str.size()!=1)return false;
    char c=str[0];
    return ('a'<=c&&c<='z')||('A'<=c&&c<='Z');
}
ll gv(char c){
    if('a'<=c&&c<='z')return c-'a';
    return c-'A'+26;
}
/*====================*/
void Solve(){
    ll n;
    cin>>n;
    vector<string>aa(n);
    for(int i=0;i<n;i++){
        string x;
        cin>>x;
        aa[i]=x;
    }
    unordered_map<string,ll>fID;
    ll fcnt=0;
    for (const string& x:aa){
        if(iv(x))continue;
        if (!fID.count(x))
            fID[x] = fcnt++;
    }
    constexpr ll fos=53;
    vector<vector<ull>>hashhhh(n+1);
    for(int l=0;l<n;l++){
        ll rID[52];
        fill(rID,rID+52,-1);
        ll nxtv = 1;
        vector<ll>nor;
        nor.reserve(n - l);
        for (int r=l;r<n;r++){
            const string& x=aa[r];
            if(iv(x)){
                ll v=gv(x[0]);
                if(rID[v]==-1){
                    rID[v]=nxtv;nxtv++;
                }
                nor.push_back(rID[v]);
            }
            else{
                nor.push_back(fos+fID[x]);
            }
        }
        C_DoubleHash hasher;
        hasher.Init(nor);
        for (int len=1;len<=(ll)nor.size();++len){
            ull value=hasher(0,len-1);
            hashhhh[len].push_back(value);
        }
    }
    for (int len=1;len<=n;++len){
        auto& cur=hashhhh[len];
        sort(cur.begin(),cur.end());
        cur.erase(unique(cur.begin(), cur.end()),cur.end());
    }
    ll q,m;
    cin>>q;
    while(q--){
        cin>>m;
        vector<string>query(m);
        for (string& x:query)cin>>x;
        if(m>n){
            cout<<"no\n";
            continue;
        }
        ll rID[52];
        fill(rID,rID+52,-1);
        ll nxtv=1;
        vector<ll> nor;
        nor.reserve(m);
        bool pos=true;
        for (const string& x:query){
            if(iv(x)){
                ll v=gv(x[0]);
                if (rID[v]==-1){
                    rID[v]=nxtv;
                    ++nxtv;
                }
                nor.push_back(rID[v]);
            }else{
                auto iterator=fID.find(x);
                if (iterator == fID.end()){
                    pos=false;
                }else{
                    nor.push_back(fos+iterator->second);
                }
            }
        }
        if(!pos){
            cout<<"no\n";
            continue;
        }
        C_DoubleHash hasher;
        hasher.Init(nor);
        ull queryHash = hasher(0, m-1);
        bool found=binary_search(hashhhh[m].begin(),hashhhh[m].end(),queryHash);
        cout<<(found?"yes\n":"no\n");
    }
    return;
}
/*====================*/
int main(){
    ios::sync_with_stdio(false);
    cin.tie(NULL);cout.tie(NULL);
    int T=1;
    while(T--)Solve();
    return 0;
}
原文