92分MLE求助

P4683 [IOI2008] Type Printer

OMG_NOIP @ 2024-05-11 13:40:59

rt,帮帮忙吧QWQ

#include<bits/stdc++.h>
#define ll int
using namespace std;
ll n,tri[450000][26],pot;
map<ll,ll>cnt;
void insert(string s){
    ll p=0;
    for(ll i=0;i<s.size();i++){
        ll to=(ll)(s[i]-'a');
        if(tri[p][to]==0)tri[p][to]=++pot;
        p=tri[p][to];
    }
    cnt[p]++;
}
vector<pair<ll,ll>>qwe[450000];
ll dfs_1(ll p,ll now){
    ll res=now;
    for(ll j=0;j<=25;j++){
        if(tri[p][j]==0)continue;
        ll asd=dfs_1(tri[p][j],now+1);
        res=max(res,asd);
        qwe[p].push_back({j,asd});
    }
    return res;
}
bool cmp(pair<ll,ll>x,pair<ll,ll>y){
    return x.second<y.second;
}
vector<char>ans;
void dfs_2(ll p){
    if(cnt.find(p)!=cnt.end()){
        for(ll j=1;j<=cnt[p];j++)ans.push_back('P');
        n-=cnt[p];  
    }
    if(n==0){
        cout<<ans.size()<<endl;
        for(char it:ans)cout<<it<<endl;
    }
    for(pair<ll,ll>it:qwe[p]){
        ans.push_back((char)('a'+it.first));
        dfs_2(tri[p][it.first]);
    }
    ans.push_back('-');
}
signed main(){
    ios::sync_with_stdio(false);
    cin>>n;
    for(ll j=1;j<=n;j++){
        string s;
        cin>>s;
        insert(s);
    }
    dfs_1(0,0);
    for(ll i=0;i<=pot;i++){
        sort(qwe[i].begin(),qwe[i].end(),cmp);
    }
    dfs_2(0);
    return 0;
} 

by jiangjiangQwQ @ 2024-05-12 18:26:54

@114514zll 试试 unsigned int


by OMG_NOIP @ 2024-05-13 09:31:14

@Super_excavator 不行呢,但还是谢谢你的回复。


|