QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#729925#9558. The Devilucup-team3161#TL 2ms15164kbC++174.5kb2024-11-09 18:02:072024-11-09 18:02:08

Judging History

你现在查看的是最新测评结果

  • [2024-11-09 18:02:08]
  • 评测
  • 测评结果:TL
  • 用时:2ms
  • 内存:15164kb
  • [2024-11-09 18:02:07]
  • 提交

answer

#include <bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(int i=(a);i>=(b);--i)
using namespace std;
typedef long long ll;

namespace mcmf {
    using pr = pair<ll,int>;
    const int N = 1e6+5, M = 1e6+10;
    struct edge {
        int to,nxt,v,f;
    }e[M<<1];
    int h[N], num=1;
    inline void link(int x,int y,int v,int f){
        e[++num] = {y, h[x], v, f}, h[x] = num;
        e[++num] = {x, h[y], 0, -f}, h[y] = num;
    }
    ll d[N], dis[N];
    int vis[N], fr[N];
    bool dijkstra(int s,int t){
        priority_queue<pr, vector<pr>, greater<pr> > Q;
        For(i,s,t) dis[i]=d[i],d[i]=1e18,vis[i]=fr[i]=0;
        Q.emplace(d[s]=0, s);
        while (!Q.empty()){
            int x=Q.top().second; Q.pop();
            if (vis[x]) continue;
            vis[x]=1;
            for (int i = h[x]; i; i = e[i].nxt) {
                const ll v = e[i].f + dis[x] - dis[e[i].to];
                if (e[i].v && d[e[i].to] > d[x] + v) {
                    fr[e[i].to] = i;
                    Q.emplace(d[e[i].to] = d[x] + v, e[i].to);
                }
            }
        }
        For(i,s,t) d[i] += dis[i];
        return d[t] < 1e17;
    }
    pair<ll,ll> EK(int s,int t) {
        ll f=0,c=0;
        for (;dijkstra(s,t);) {
            ll fl = 1e18;
            for (int i=fr[t];i;i=fr[e[i^1].to]) fl = min((ll)e[i].v,fl);
            for (int i=fr[t];i;i=fr[e[i^1].to]) e[i].v-=fl,e[i^1].v+=fl;
            f+=fl,c+=fl*d[t];
        }
        return make_pair(f,c);
    }
}

const int N = 130, M=1e5+5, base=998244353;
const ll MOD=971635031393707027;

#define lll __int128
#define eb emplace_back
#define fi first
#define se second

struct Dat
{
    int len;ll hs;
    int pr1,pr2,pr3;
};

int n,tp;vector<Dat> z1,z[N][N];
int cnt;string w[M];map<string,int> id;
vector<Dat> trs(vector<Dat> a,int pr1,int pr2)
{
    for(int i=0;i<a.size();++i)
        a[i].pr1=pr1,a[i].pr2=pr2,a[i].pr3=i;
    return a;
}
void merge(vector<Dat> &a,vector<Dat> b)
{
    int j=0;
    vector<Dat> t;
    for(int i=0;i<a.size();++i)
    {
        while(j<b.size() && (a[i].len>b[j].len || (a[i].len==b[j].len && a[i].hs>b[j].hs)))
            t.eb(b[j++]);
        t.eb(a[i]);
    }
    while(j<b.size()) t.eb(b[j++]);
    a.clear();
    for(int i=0;i<t.size() && a.size()<n;++i)
        if(!i || t[i].hs!=t[i-1].hs)
            a.eb(t[i]);
}
int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin>>n;cin.get();
    for(int x=1;x<=n;++x)
    {
        string a,b1;
        vector<string> b;
        getline(cin,a);
        for(int i=0;i<a.size();++i)
            if(a[i]==' ') b.eb(b1),b1.clear();
            else b1+=a[i];
        b.eb(b1);
        for(int i=0;i<b.size();++i)
            for(int j=0;j<b[i].size();++j)
                z[i][j].clear();
        z1.clear();
        for(int i=0;i<b.size();++i)
        {
            if(i)
            {
                for(int j=0;j<b[i-1].size();++j)
                    for(auto k:z[i-1][j])
                        merge(z[i][0],trs(z[i-1][j],i-1,j));
            }
            else z[i][0].eb((Dat) {0,0,-1,-1,-1});
            for(auto &k:z[i][0]) ++k.len,k.hs=((lll)k.hs*base+b[i][0])%MOD;
            for(int j=1;j<b[i].size();++j)
            {
                z[i][j]=trs(z[i][j-1],i,j-1);
                for(auto &k:z[i][j]) ++k.len,k.hs=((lll)k.hs*base+b[i][j])%MOD;
            }
        }
        int t=b.size()-1;
        for(int i=0;i<b[t].size();++i)
            merge(z1,trs(z[t][i],t,i));
        for(auto i:z1)
        {
            string a;
            for(int j=i.pr1,k=i.pr2,l=i.pr3,j1,k1,l1;j!=-1;)
            {
                a+=b[j][k];
                j1=z[j][k][l].pr1;
                k1=z[j][k][l].pr2;
                l1=z[j][k][l].pr3;
                j=j1;k=k1;l=l1;
            }
            reverse(a.begin(),a.end());
            //cout<<a<<endl;
            if(!id.count(a)) id[a]=++cnt,w[cnt]=a;
            mcmf::link(x,n+id[a],1,i.len);
        }
    }
    for(int i=1;i<=n;++i) mcmf::link(0,i,1,0);
    for(int i=1;i<=cnt;++i) mcmf::link(n+i,n+cnt+1,1,0);
    pair<ll,ll> f=mcmf::EK(0,n+cnt+1);
    if(f.fi<n) {cout<<"no solution\n";return 0;}
    for(int i=1;i<=n;++i)
    {
        for(int j=mcmf::h[i];j;j=mcmf::e[j].nxt)
        {
            int k=mcmf::e[j].to;
            if(!mcmf::e[j].v && k>=n+1 && k<=n+cnt)
                cout<<w[k-n]<<'\n';
        }
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 14164kb

input:

5
automated teller machine
active teller machine
active trouble maker
always telling misinformation
American Teller Machinery

output:

autm
atem
atma
atm
ATM

result:

ok len=18

Test #2:

score: 0
Accepted
time: 0ms
memory: 13616kb

input:

5
Forest Conservation Committee Forum
Fuming Corruption Collusion Federation
Fulsome Cash Concealment Foundation
Funky Crony Capitalism Facilitator
Funny Cocky Cocky Funny

output:

FoCCF
FCCoF
FCCFo
FCCF
FCoCF

result:

ok len=24

Test #3:

score: 0
Accepted
time: 0ms
memory: 15164kb

input:

3
A AA
AA A
A A A

output:

no solution

result:

ok len=-1

Test #4:

score: 0
Accepted
time: 2ms
memory: 14416kb

input:

2
QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnmertyuiop
Q W E R T Y U I O P A S D F G H J K L Z X C V B N M q w e r t y u i o p a s d f g h j k l z x c v b n m j k l z x c v b n m

output:

Q
QWERTYUIOPASDFGHJKLZXCVBNMqwertyuiopasdfghjklzxcvbnmjklzxcvbnm

result:

ok len=63

Test #5:

score: 0
Accepted
time: 2ms
memory: 14448kb

input:

10
aaa aaa aaa aaa aaa aaa
aab aaa aaa aaa aaa aaa
aaa aab aaa aaa aaa aaa
aab aab aaa aaa aaa aaa
a a a a a a
ab ab a a a a a a
ab ab b a a a a a a
aw a a a a a
az az a a a a
az a a a a a

output:

aaaaaaaaa
aaaaaaa
aaabaaaa
aabaaaaa
aaaaaa
aaaaaaaa
aabaaaaaa
awaaaaa
aazaaaa
azaaaaa

result:

ok len=76

Test #6:

score: -100
Time Limit Exceeded

input:

128
zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz zzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzzz...

output:


result: