题解:P12597 穿睡衣军训

· · 题解

这题挂了 140pts /fad

为方便表述,定义 n=|s|,m=|t|。

注意到 n\le 10^3,考虑 O(n^2) 枚举子串。

然后很容易写出 O(n\log m) 的判断子序列,这样就得到了一个过不了的 O(Tn^3\log m) 做法。

考虑优化,注意到判断子序列其实是可以继承于上一段的结果的,于是我们少了一个判断子序列花费的 n,此时复杂度来到 O(Tn^2\log m),由于可以剪枝加上小常数,此时可以通过。

实现没啥难度,但是有个笨蛋赛时挂了。

#include<bits/stdc++.h>
using namespace std;
void solve(){
    string s,t;
    cin>>s>>t;
    int n=s.size(),m=t.size();
    s=" "+s;
    t=" "+t;
    vector<int>ve[35];
    for(int i=1;i<=m;i++)
    ve[t[i]-'a'].push_back(i);
    string ans;
    for(int i=1;i<=n&&n-i+1>=ans.size();i++){
        string flc;
        for(int j=i,x=-1;j<=n;j++){
            auto catnip=upper_bound(ve[s[j]-'a'].begin(),ve[s[j]-'a'].end(),x);
            if(catnip==ve[s[j]-'a'].end())break;
            x=ve[s[j]-'a'][catnip-ve[s[j]-'a'].begin()];
            flc+=s[j];
            // cout<<x;
        }
        if(flc.size()>ans.size()||flc.size()==ans.size()&&flc<ans)ans=flc;
    }
    cout<<ans<<'\n';
}
int main(){
    int t;
    cin>>t;
    while(t--)solve();
    return 0;
}

有个糖诗这场比赛总共挂了 435pts,是谁啊好难猜呢。