QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#232035#6507. Recover the Stringucup-team1198#RE 989ms542148kbC++203.2kb2023-10-29 19:22:132023-10-29 19:22:15

Judging History

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

  • [2023-10-29 19:22:15]
  • 评测
  • 测评结果:RE
  • 用时:989ms
  • 内存:542148kb
  • [2023-10-29 19:22:13]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define pii pair<int, int>
#define ld long double
#define all(a) (a).begin(), (a).end()

const int MOD = 1e9 + 1000 - 7, B = 5757;

int add(int a, int b) {
    return a + b >= MOD ? a + b - MOD : a + b;
}

int mul(int a, int b) {
    return (1ll * a * b) % MOD;
}

struct Node {
    int h0, h1;
    int par;
    int symb;

    Node(int par = -1, int symb = -1): h0(0), h1(0), par(par), symb(symb) {}
};

const int MAXMEM = 1e7 + 100;
Node nd[MAXMEM];
int id_nd = 0;

int nn(int par = -1, int symb = -1) {
    nd[id_nd] = Node(par, symb);
    return id_nd++;
}

int push(int v, int ch) {
    int u = nn(v, ch);
    nd[u].h0 = add(ch, mul(nd[v].h0, B));
    if (nd[v].par != -1) {
        nd[u].h1 = add(ch, mul(nd[v].h1, B));
    }
    return u;
}

string restore(int v) {
    string ans;
    while (nd[v].par != -1) {
        ans.push_back(nd[v].symb + 'a');
        v = nd[v].par;
    }
    reverse(ans.begin(), ans.end());
    return ans;
}

int get_pref(int v) {
    return nd[nd[v].par].h0;
}

int get_suf(int v) {
    return nd[v].h1;
}

const int MAXN = 1e6 + 100;
vector<int> g[MAXN];
vector<int> var[MAXN];
int used[MAXN];

int last_letter;

int rt = nn();

void dfs(int v) {
    used[v] = true;
    for (int u : g[v]) {
        if (!used[u]) {
            dfs(u);
        }
    }
    if (g[v].empty()) {
        var[v] = {push(rt, last_letter++)};
        return;
    }
    if ((int)g[v].size() == 1) {
        int u = var[g[v][0]][0];
        var[v] = {push(u, nd[u].symb)};
        return;
    }
    int a = g[v][0], b = g[v][1];
    for (int i = 0; i < 2; ++i, swap(a, b)) {
        unordered_set<int> hsh;
        for (int p : var[a]) {
            for (int q : var[b]) {
                if (get_suf(p) == get_pref(q)) {
                    int r = push(p, nd[q].symb);
                    if (!hsh.count(nd[r].h0)) {
                        hsh.insert(nd[r].h0);
                        var[v].push_back(r);
                    }
                }
            }
        }
    }
}

void solve() {
    int n, m;
    cin >> n >> m;
    for (int i = 0; i < n; ++i) {
        g[i].clear();
    }
    for (int i = 0; i < m; ++i) {
        int u, v;
        cin >> u >> v;
        u--; v--;
        g[v].push_back(u);
    }
    fill(var, var + n, vector<int>());
    fill(used, used + n, 0);
    for (int i = 0; i < n; ++i) {
        for (int u : g[i]) {
            used[u] = true;
        }
    }
    int rt = -1;
    for (int i = 0; i < n; ++i) {
        if (!used[i]) {
            rt = i;
        }
    }
    fill(used, used + n, 0);
    last_letter = 0;
    dfs(rt);
    string ans;
    for (int v : var[rt]) {
        string s = restore(v);
        map<char, char> opt;
        int lst = 0;
        string t;
        for (auto ch : s) {
            if (!opt.count(ch)) {
                opt[ch] = 'a' + (lst++);
            }
            t.push_back(opt[ch]);
        }
        if (ans.empty() || t < ans) {
            ans = t;
        }
    }
    cout << ans << "\n";
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int tst;
    cin >> tst;
    while (tst--) {
        solve();
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 4ms
memory: 206688kb

input:

3
1 0
5 6
2 4
2 5
5 3
4 3
1 5
1 4
8 11
1 2
1 4
1 6
2 5
3 4
3 6
4 5
4 7
5 8
6 7
7 8

output:

a
aba
aaba

result:

ok 3 lines

Test #2:

score: 0
Accepted
time: 287ms
memory: 206992kb

input:

26837
1 0
2 1
2 1
3 2
1 3
2 3
3 2
3 1
1 2
5 5
4 3
5 1
1 2
3 2
5 3
5 6
2 4
2 5
5 3
4 3
1 5
1 4
5 5
1 5
2 1
2 4
4 5
3 4
6 6
1 4
5 3
2 4
4 6
3 6
2 3
4 3
1 3
3 4
2 1
7 8
2 5
1 3
7 1
3 5
7 6
1 2
4 6
6 3
8 11
2 6
2 7
3 7
8 1
6 4
4 5
7 4
7 1
3 8
2 8
1 5
8 10
8 1
4 5
7 8
3 5
3 1
7 3
1 2
5 2
6 4
6 3
9 11
5 2...

output:

a
aa
ab
aaa
aab
aba
aab
abc
aaaa
aaab
aaba
aabb
aabc
aaba
abab
abac
abba
aaab
abbc
abca
abac
aabc
abcd
aaaaa
aaaab
aaaba
aaabb
aaabc
aabaa
aabab
aabac
aabba
aaabb
aabbc
aabca
aabcb
aabcc
aabcd
aaaba
abaab
abaac
ababa
aabab
ababc
abaca
abacb
aabcb
abacd
aabba
abaab
abbac
abbba
aaaab
abbbc
abbca
abaac...

result:

ok 26837 lines

Test #3:

score: 0
Accepted
time: 309ms
memory: 206772kb

input:

6450
148 290
30 17
69 52
99 36
15 20
96 129
17 64
86 137
74 81
145 138
135 30
113 118
26 52
125 60
39 34
15 59
112 92
126 68
13 23
33 96
145 148
71 88
77 51
81 86
12 127
54 5
47 31
13 21
49 137
51 66
135 48
45 62
93 53
133 119
76 65
94 104
103 25
10 143
44 2
4 80
92 57
131 99
64 28
36 148
53 122
35 ...

output:

abbabaababbaabbabaab
abbcacababccababcbca
abcdefghijklmnopqrst
abaababaabaababaabab
abcabcabcabcabcabcab
abaabaabaabaabaabaab
ababaababaababaababa
abaabaabaabaababaaba
abababababababababab
abacabacabacabacabac
abacabadabacabaeabac
aabaabbabbbaabbaabba
aaababbcbbccaaaabcac
abcadefghijfklmkcbdf
aabaca...

result:

ok 6450 lines

Test #4:

score: 0
Accepted
time: 316ms
memory: 207088kb

input:

970
920 1834
730 734
56 197
114 794
161 101
228 454
791 135
423 9
877 886
875 387
840 727
730 643
587 823
112 816
672 87
911 365
251 329
478 863
76 580
869 898
906 598
222 116
523 621
349 645
601 897
109 271
26 594
877 691
409 891
245 286
146 591
289 95
786 850
576 8
131 507
6 285
71 695
853 713
265...

output:

ababbabaabbaababbaabbabaabbaababbabaababbaabbabaab
ababccababcbcaabcbcacabcababcbcaabcbcacabbcacababc
abcdefghijklmnopqrstuvwxyabcdefghijklmnopqrstuvwxz
abaababaabaababaababaabaababaabaababaababaabaababa
abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcab
abaabaabaabaabaabaabaabaabaabaabaabaabaabaaba...

result:

ok 970 lines

Test #5:

score: 0
Accepted
time: 378ms
memory: 207120kb

input:

237
3672 7338
128 799
1687 1156
3289 2676
17 11
10 2456
3634 1671
1180 2885
1838 3438
272 406
1704 2120
2612 2145
3172 1407
2717 3154
2801 1157
1377 2383
3458 3086
1633 3374
1922 869
958 248
2150 2186
1010 2998
370 1368
3407 2495
3644 987
1582 1701
2231 3541
1600 1620
2574 2836
3238 2131
1373 1548
2...

output:

abbaabbabaabbaababbabaababbaabbabaababbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababba
aabcbcacabbcacababcbcacababccababcbcaabcbcacabcababcbcaabcbcacabbcacababcabcbcacabbcacababccababcbca
abcdefghijklmnopqrstuvwxyabcdefghijklmnopqrstuvzxyabcdefghijklmnopqrstuvzwyabcdefghijklmnopqrstuvz...

result:

ok 237 lines

Test #6:

score: 0
Accepted
time: 447ms
memory: 210048kb

input:

30
33368 66730
8076 11100
29514 11018
19224 28482
7851 20178
15965 14582
27252 7790
21764 13370
14476 32499
32535 26445
13036 10117
21957 8939
27288 2575
94 31016
28912 8162
11851 20676
1998 14167
23076 4834
13894 23588
4510 16856
4125 24548
7174 31028
20038 16939
13363 29338
27074 24233
14616 29951...

output:

abbaabbabaabbaababbaabbabaababbabaabbaababbabaababbaabbabaababbabaabbaababbaabbabaabbaababbabaababbaabbabaababbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbaabbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbabaababbaabbabaababbabaabbaababbaabbabaabbaababbabaababbaabbabaab...

result:

ok 30 lines

Test #7:

score: 0
Accepted
time: 629ms
memory: 234848kb

input:

8
380248 760490
124441 243214
318264 317073
80812 184051
371269 92314
147997 324777
348208 151765
277437 22744
135856 228557
170049 232045
211622 83057
272356 232823
47448 177829
101380 373767
357788 184436
373026 30555
89629 377654
213780 51067
227975 39349
216809 210156
42077 295276
195939 221049
...

output:

abbabaabbaababbaabbabaababbabaabbaababbabaababbaabbabaababbabaabbaababbaabbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbaabbabaabbaababbabaababbaabbabaababbabaabbaababbabaababbaabbabaabbaababbaabbabaababbabaabbaababbabaababbaabbabaababbabaabbaababbaabbabaabbaababbabaababbaabbabaababba...

result:

ok 8 lines

Test #8:

score: 0
Accepted
time: 601ms
memory: 542084kb

input:

1
1000000 999999
486593 40134
567630 575732
673482 763704
372903 393266
657204 514133
677524 433408
258586 60505
668823 278755
406509 351060
877657 86847
393838 390004
323007 188430
833631 698149
362171 368078
725792 944166
810862 211037
579856 958663
293964 656704
398325 41189
393451 571249
585688 ...

output:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

result:

ok single line: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'

Test #9:

score: 0
Accepted
time: 973ms
memory: 273720kb

input:

1
999720 1999418
478960 367385
278842 195576
978938 976238
621173 945176
934972 578856
824323 381756
538870 587404
445290 681032
934823 385654
924723 412221
85667 342451
175667 376718
825906 546292
215330 897312
29111 214255
903178 224277
622872 949028
895275 674752
744266 238233
917807 485857
50574...

output:

ababbaaaabbaaababbbabaaababaabbaababbbabbbaababbaaaabbabbbbbababbbaababaababaabbaabbbbbaabaababbbbbbbaaabbabaaabbaabbbababaabaabbbaabbabababbababababbbaabbbbbbbabbbabbbbbaabbaaaaabbbbabbaaaaababaabbababaababababbababbaabbabaaaababaabbababbbbaaaabaaabbaababaaaabbbabaaaaabbbaaabbaababbbaaaabaabbbbaabb...

result:

ok single line: 'ababbaaaabbaaababbbabaaababaab...abbbaabaababbaaabbaabbbababbbba'

Test #10:

score: 0
Accepted
time: 970ms
memory: 273476kb

input:

1
999962 1999902
52152 909124
778034 197109
215551 980572
351009 284250
906426 112777
146723 864320
128930 437120
297457 358563
702334 350219
168315 3105
750352 326219
541338 400633
725295 147687
463415 95558
557045 884970
906696 895067
8701 56935
472957 784385
733678 464051
491501 368221
717902 907...

output:

aababbaaabbaaaabbabaabbbabbaabaaaabbabbabbbbabbbaaabbbbbbaabbaabbabbabbabaaaabaabbaaababbbbbaaababaaaababaabbababbbababbabaaaaaaabbaabbababbaabbaabaabababbaaabaaaaababababbbabbaabaaababbbaaaabaababbbbbaababbabbaaaabbbabbbbbaabaaabaaabbaaabbbbababbaabbbbbbbabababbbbaabaaaaabbbabbbbabaaaaabbaabbabbbaa...

result:

ok single line: 'aababbaaabbaaaabbabaabbbabbaab...aababaaaaaabbabaabbbbaabbaabbab'

Test #11:

score: 0
Accepted
time: 989ms
memory: 273532kb

input:

1
999932 1999842
994049 660788
387551 736624
508811 633488
317145 571345
906407 201785
430886 611119
183526 415265
720900 956714
207924 571189
781568 293026
258860 300050
29668 968606
711768 267147
140099 193742
353070 17072
117128 701452
842704 198461
918561 757129
879182 813948
245889 282501
46994...

output:

aabaaabababbabbababbbaaabbababaabaabaabbababbbababababbbabbbaabbabaabbaaaaaabababbaaababbabbabbabaaabaabbabaaabbbabbaabaaaaaaaabaabbaababbaaababababbbabaaaaaababbbabbbabbabbbabbaaaababbbbabbbabbaababaabbbabaaabbabaaabbaaabababbbabaaabbaaababbbaaababbaabbbabbbbbabbbabbbabbbbabaaaaaaabaababbaaabababaa...

result:

ok single line: 'aabaaabababbabbababbbaaabbabab...baabaaababbaaaaabaaabbbaaaabbaa'

Test #12:

score: 0
Accepted
time: 950ms
memory: 273484kb

input:

1
999960 1999898
696875 9265
618298 741593
334125 932167
316155 284255
923880 106712
771194 353262
31998 417868
845589 256875
591867 976859
220019 209003
829310 14740
460460 37659
538159 801865
462765 878326
602060 979546
157850 511382
761195 500165
433201 295913
87888 947492
306572 761401
978020 91...

output:

aaaaabbabaabbbabababbaaaabbaaaabaabaababbabbbaabbaaabbbbaaaaabababbaabaabbbabbaaabbabbaabbaabaaabbaaaababbababaabbbaabaababaabbbbabbbbbbaabbaababbabbbbbabbaabbaabaaababbaaaaaabbbaaaaaabbababaabaaababbabaaaabbbbbababbabaabbabbabbaaabaaaaabaaaabbabaaababbabbbbbbaaaabaabbaababbaaaaaaaababbaababaaabbbab...

result:

ok single line: 'aaaaabbabaabbbabababbaaaabbaaa...bbbbbbbbabaaaaaaaaabbabbaaaabbb'

Test #13:

score: 0
Accepted
time: 982ms
memory: 273492kb

input:

1
999286 1998549
332293 847900
781086 328251
89438 882788
175083 14440
572430 574257
809569 484269
663930 438265
122446 76616
651689 649277
10930 366807
865522 459778
415599 426786
191310 197100
747014 674992
395012 954730
372947 143720
681954 55327
377093 403183
960062 910427
919504 396335
314041 2...

output:

aabbacbacbccccabcacccabbbabcccacbaaaaaaccacbccaacbbaaacbcbaccbbacaacabcaccccaabcabccaaccbaaabbabbccbbbcbcbbbccababbbbbcaabbacaaacaabbaacbaabbbbbcbccaccbbcbabcbacccabcbbcbcaaabbbabbaccacacbaacaabbaacccacbaabbccbbcabacabbcbababccbbcbaacacbaccaaabbabbbbbaacbbbaaabacaabcacaaaccaaccbcababacbbabbababacbcc...

result:

ok single line: 'aabbacbacbccccabcacccabbbabccc...acbacbbabaaacaccbccaabbbbaaaaba'

Test #14:

score: 0
Accepted
time: 976ms
memory: 273488kb

input:

1
999331 1998641
729147 538731
921253 798894
885546 677818
298747 97157
180087 234737
418721 783414
901052 913813
595106 405323
295245 567060
70011 388501
377766 19230
874327 848890
928988 650497
238183 939267
593826 388828
573609 454635
81052 5415
661081 742732
500902 266652
996206 520864
328011 14...

output:

aabacbaaccbabbabbacaccbbabcbccabcabbbacbccacbabccbbcacbabbcbbabbacacccaaccccbaaccbaabcbcaaaaaacacbccbbcaabcbaabaccbcabacaccbbbabccabaaabbbbbcbacacacaabcacaabccccabcaabaacacbccabaaccabaccaabcacbccbbabbacbbcbabccaabbbcbacbbbcabaabcabacbaaccaccacabcacbaccaabacacccbabaacbbbcbcabcbabcababbaabacaacacccacb...

result:

ok single line: 'aabacbaaccbabbabbacaccbbabcbcc...cacacacaaaaacbbaaaccbaacbccaaca'

Test #15:

score: 0
Accepted
time: 982ms
memory: 273440kb

input:

1
999390 1998754
371866 116100
905495 392764
44483 862055
894183 603825
297556 557791
204825 860195
258135 783387
213801 233286
733327 526541
291442 435626
497561 467258
125745 889357
162794 595141
727020 316815
329022 931460
658882 219351
855248 963142
453518 646179
49612 456747
168958 167848
23953...

output:

aabbbccaaddebdeebadbabadcdddbacaacababeeabbeebddadedaddaadedcdebccabeaebcdbaabceaceebabeeeddeecaecccddabdbbacadaedceddbecaecdcbdaebbdbaacaaebddaeaaabaddbecaacacacedccdabbaaacdeaccacdbaeeecaecbeddcdaecabceaebdebaeceebbbcdbdeacdcbacceaaebeecbecdaadcabbdbcadbaaeabcdaeabcdcaabdbcebecacebcdceeeacebbabada...

result:

ok single line: 'aabbbccaaddebdeebadbabadcdddba...eeccdaabdcbadbdcbaecedbdccdaeba'

Test #16:

score: 0
Accepted
time: 958ms
memory: 273500kb

input:

1
999332 1998641
340224 312609
496357 621563
282231 31016
9727 518497
345711 368257
231510 162608
370656 157276
520228 905900
444817 419597
284400 368374
619796 817878
542290 224955
388559 105743
824688 371866
837389 734221
600626 944539
93565 550551
676681 298291
663021 283487
761202 315067
748929 ...

output:

aaaabbcaddcacaecdedaadbcbeadddbebbedeededceddedcadadacadceddbddbcadcdabedaabdbccededaccedabecacbacdcacebaecaabbdeedcdaacaacaceaeeedeecbbbeedacdeaaebcadaddcdecaadebabbcbcbbddcadcbeabeaededbcacdbbdcabeaaeaaedabdbcbeadaddadedddaebcacacbdbdedcccaabbbdaebdadecbecabaedadcbceecbdadacdbbdabeaaddebcbacdcbaca...

result:

ok single line: 'aaaabbcaddcacaecdedaadbcbeaddd...eacabbeddaaebbabcadcccbccbacdaa'

Test #17:

score: 0
Accepted
time: 972ms
memory: 273400kb

input:

1
999739 1999440
205098 344014
307300 441050
151960 549625
675401 498542
254496 388186
804623 686320
449469 974202
149674 935006
81205 629864
388824 158142
4307 138602
862844 464387
337179 904964
437580 558766
679226 445604
698011 971021
661578 367602
698306 240195
179735 321954
638187 260543
541268...

output:

ababcdefdgfcdafhhceahiifjgafafcigfgfagjdjigccjgfcjifaegcghjiadafegiaajfbjjbajdddjjjfggggjgaccdchcafcddbhcfchbjjcjfjbihebdjbbcfafbfhdfhibdjhcgajahjibfgicfibadjacggejebbgjijaafdidgbajgidffahebabbghecffcfgiaficgichiffadidjfheaibhfbhjegeghjbecdjdjjhadjaieieeebagcehdicjdgbdadgceaaefahgfjjaebiajdjdibeheeb...

result:

ok single line: 'ababcdefdgfcdafhhceahiifjgafaf...cfhdacdjbjjddfgcabahhcdeiicjjce'

Test #18:

score: 0
Accepted
time: 987ms
memory: 273516kb

input:

1
999710 1999383
937916 289472
397745 223193
993084 450771
753199 164546
143627 180490
834071 338355
930758 765524
949290 91931
660057 139978
516021 518052
871186 290530
170047 970212
327356 621467
28958 564133
69866 550611
793613 404090
468057 410337
75929 627503
834050 483273
545988 569261
672096 ...

output:

aabcdefgfbhhccdeigdgjbffiadhbfiidghdehbddedajggcahedibjegediaeeafjbiedbgaagfehgigbghdgbjjfcbggfgcdghahahijjiagaiihbeiiefchhhgdjchecefccfhdadfeffibffhebdbjicccgehhchdcfjibbjbajbcigefgiahjhahgdahfjjifjgcjbggjbbhhhahbebbaeeajabcgahjiaajaaejcjgffhcadeggjhdibddggjibajejdbaahiigbdcdafbcdebjiedbgjbidfhddgg...

result:

ok single line: 'aabcdefgfbhhccdeigdgjbffiadhbf...jgaagcajigbaejchgeebccacidafbfd'

Test #19:

score: 0
Accepted
time: 551ms
memory: 542148kb

input:

1
1000000 999999
278296 106049
911981 972246
893106 498127
783362 841190
100615 892567
336171 546759
879578 798806
821558 165334
240626 948176
766870 782719
698585 659818
433374 475363
210776 45956
160561 881213
929083 451179
276955 528871
172610 1150
945837 980812
612046 510874
516551 688101
401500...

output:

aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

result:

ok single line: 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa'

Test #20:

score: -100
Runtime Error

input:

1
999999 1999994
581178 432299
504497 15192
717769 924100
36894 56780
350786 794293
246364 706614
179777 929163
22940 704272
114548 224999
620984 147378
124534 370669
505076 273968
57565 156162
541654 824947
916216 571797
789038 941852
350195 648312
405505 10481
468099 554657
404064 883737
532801 98...

output:


result: