CSES - Aalto Competitive Programming 2024 - wk9 - Mon - Results
Submission details
Task:Buzzwords
Sender:odanobunaga8199
Submission time:2024-11-04 17:06:58 +0200
Language:C++ (C++20)
Status:READY
Result:ACCEPTED
Test results
testverdicttime
#1ACCEPTED0.00 sdetails
#2ACCEPTED0.00 sdetails
#3ACCEPTED0.00 sdetails
#4ACCEPTED0.00 sdetails
#5ACCEPTED0.00 sdetails
#6ACCEPTED0.00 sdetails
#7ACCEPTED0.00 sdetails
#8ACCEPTED0.00 sdetails
#9ACCEPTED0.00 sdetails
#10ACCEPTED0.00 sdetails
#11ACCEPTED0.00 sdetails
#12ACCEPTED0.00 sdetails
#13ACCEPTED0.01 sdetails
#14ACCEPTED0.00 sdetails
#15ACCEPTED0.00 sdetails
#16ACCEPTED0.00 sdetails
#17ACCEPTED0.00 sdetails
#18ACCEPTED0.00 sdetails
#19ACCEPTED0.00 sdetails
#20ACCEPTED0.00 sdetails
#21ACCEPTED0.01 sdetails
#22ACCEPTED0.00 sdetails
#23ACCEPTED0.00 sdetails
#24ACCEPTED0.00 sdetails
#25ACCEPTED0.01 sdetails
#26ACCEPTED0.00 sdetails
#27ACCEPTED0.01 sdetails
#28ACCEPTED0.00 sdetails
#29ACCEPTED0.00 sdetails
#30ACCEPTED0.00 sdetails
#31ACCEPTED0.01 sdetails
#32ACCEPTED0.01 sdetails
#33ACCEPTED0.00 sdetails
#34ACCEPTED0.00 sdetails
#35ACCEPTED0.01 sdetails
#36ACCEPTED0.00 sdetails
#37ACCEPTED0.01 sdetails
#38ACCEPTED0.00 sdetails
#39ACCEPTED0.00 sdetails
#40ACCEPTED0.00 sdetails
#41ACCEPTED0.07 sdetails
#42ACCEPTED0.06 sdetails
#43ACCEPTED0.05 sdetails
#44ACCEPTED0.01 sdetails

Code

#include <bits/stdc++.h>
using namespace std;

const int ALPHABET_SIZE = 27;

int charToIndex(char c) {
    if (c == ' ') return 26;
    return c - 'a';
}

struct Node {
    int children[ALPHABET_SIZE];
    int fail;
    vector<int> output;
    Node() : fail(0) {
        for(int i=0;i<ALPHABET_SIZE;i++) children[i] = -1;
    }
};

int main(){
    ios::sync_with_stdio(false);
    cin.tie(0);
    
    string s;
    getline(cin, s);
    
    int q;
    cin >> q;
    cin.ignore();
    
    vector<Node> trie;
    trie.emplace_back();
    
    vector<int> counts(q, 0);
    
    for(int i=0;i<q;i++){
        string pattern;
        getline(cin, pattern);
        int node = 0;
        for(char c: pattern){
            int idx = charToIndex(c);
            if(trie[node].children[idx] == -1){
                trie[node].children[idx] = trie.size();
                trie.emplace_back();
            }
            node = trie[node].children[idx];
        }
        trie[node].output.push_back(i);
    }
    
    queue<int> qNodes;
    for(int i=0;i<ALPHABET_SIZE;i++){
        if(trie[0].children[i] != -1){
            trie[trie[0].children[i]].fail = 0;
            qNodes.push(trie[0].children[i]);
        }
        else{
            trie[0].children[i] = 0;
        }
    }
    
    while(!qNodes.empty()){
        int current = qNodes.front();
        qNodes.pop();
        for(int i=0;i<ALPHABET_SIZE;i++){
            if(trie[current].children[i] != -1){
                int child = trie[current].children[i];
                int fail = trie[current].fail;
                while(trie[fail].children[i] == -1 && fail != 0){
                    fail = trie[fail].fail;
                }
                if(trie[fail].children[i] != -1){
                    fail = trie[fail].children[i];
                }
                trie[child].fail = fail;
                for(auto pid: trie[fail].output){
                    trie[child].output.push_back(pid);
                }
                qNodes.push(child);
            }
            else{
                trie[current].children[i] = trie[trie[current].fail].children[i];
            }
        }
    }
    
    int node = 0;
    for(char c: s){
        int idx = charToIndex(c);
        while(trie[node].children[idx] == -1 && node != 0){
            node = trie[node].fail;
        }
        if(trie[node].children[idx] != -1){
            node = trie[node].children[idx];
        }
        else{
            node = 0;
        }
        for(auto pid: trie[node].output){
            counts[pid]++;
        }
    }
    
    long long total = 0;
    for(auto cnt: counts){
        total += cnt;
    }
    cout << total;
}

Test details

Test 1

Verdict: ACCEPTED

input
g fhe
395
a
aa
aafh
...

correct output
10

user output
10

Test 2

Verdict: ACCEPTED

input
s adh
372
a
ad
ae
...

correct output
6

user output
6

Test 3

Verdict: ACCEPTED

input
adcdb
20
a
aa
ab
...

correct output
9

user output
9

Test 4

Verdict: ACCEPTED

input
aaaaa
5
a
aa
aaa
...

correct output
15

user output
15

Test 5

Verdict: ACCEPTED

input
o zws
759
a
ab
abw
...

correct output
8

user output
8

Test 6

Verdict: ACCEPTED

input
aaaaa
5
a
aa
aaa
...

correct output
15

user output
15

Test 7

Verdict: ACCEPTED

input
ifv b
649
a
aaw
ac
...

correct output
10

user output
10

Test 8

Verdict: ACCEPTED

input
dbbdc
129
a
aa
aaab
...

correct output
15

user output
15

Test 9

Verdict: ACCEPTED

input
aaaaa
5
a
aa
aaa
...

correct output
15

user output
15

Test 10

Verdict: ACCEPTED

input
eeeab
9
a
b
c
...

correct output
5

user output
5

Test 11

Verdict: ACCEPTED

input
ghfhe d fd
430
a
aa
aafa
...

correct output
22

user output
22

Test 12

Verdict: ACCEPTED

input
syadhzdgck
275
a
ad
adh
...

correct output
53

user output
53

Test 13

Verdict: ACCEPTED

input
adcdbbbbba
256
a
aa
aaa
...

correct output
49

user output
49

Test 14

Verdict: ACCEPTED

input
aaaaaaaaaa
10
a
aa
aaa
...

correct output
55

user output
55

Test 15

Verdict: ACCEPTED

input
o zwspspfd
468
a
adks
afhsrhvwxn
...

correct output
45

user output
45

Test 16

Verdict: ACCEPTED

input
aaaaaaaaaa
10
a
aa
aaa
...

correct output
55

user output
55

Test 17

Verdict: ACCEPTED

input
i vbbjczpc
703
a
aa
abou
...

correct output
29

user output
29

Test 18

Verdict: ACCEPTED

input
d bdcbdbcb
128
a
aa
aab
...

correct output
42

user output
42

Test 19

Verdict: ACCEPTED

input
aaaaaaaaaa
10
a
aa
aaa
...

correct output
55

user output
55

Test 20

Verdict: ACCEPTED

input
eeeabcbabh
171
a
aac
aahg
...

correct output
34

user output
34

Test 21

Verdict: ACCEPTED

input
ghfhehdf ddciaicdehheefdihadaf...

correct output
246

user output
246

Test 22

Verdict: ACCEPTED

input
s adhzdgckekirkyowkirnflwfanrx...

correct output
52

user output
52

Test 23

Verdict: ACCEPTED

input
adcdbbbbbaaccabbbccd cabccacdb...

correct output
178

user output
178

Test 24

Verdict: ACCEPTED

input
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

correct output
2518

user output
2518

Test 25

Verdict: ACCEPTED

input
oezws spfdzfasgxlquafowjzpe pv...

correct output
284

user output
284

Test 26

Verdict: ACCEPTED

input
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

correct output
1387

user output
1387

Test 27

Verdict: ACCEPTED

input
ifvbbjczpcnpknimqvlttkngpe szx...

correct output
258

user output
258

Test 28

Verdict: ACCEPTED

input
dbbdcbdb bcaabb bccddbbbacbcdc...

correct output
36

user output
36

Test 29

Verdict: ACCEPTED

input
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

correct output
3076

user output
3076

Test 30

Verdict: ACCEPTED

input
e eabcbabhdbcbaddebchdiga ggfb...

correct output
23

user output
23

Test 31

Verdict: ACCEPTED

input
ghfhehdff dci icdehheefdih daf...

correct output
248

user output
248

Test 32

Verdict: ACCEPTED

input
s a hzdgckekirkyo kirnflw anrx...

correct output
27

user output
27

Test 33

Verdict: ACCEPTED

input
adcdbbb baac abbbcc c abccacd ...

correct output
433

user output
433

Test 34

Verdict: ACCEPTED

input
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

correct output
3427

user output
3427

Test 35

Verdict: ACCEPTED

input
oezwsp pf z asgxlquafowjzpekpv...

correct output
359

user output
359

Test 36

Verdict: ACCEPTED

input
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

correct output
2126

user output
2126

Test 37

Verdict: ACCEPTED

input
ifvbbjczpcnpk imqvlttkngpeqszx...

correct output
395

user output
395

Test 38

Verdict: ACCEPTED

input
dbbdcbd cbcaabbabccddbb acbcdc...

correct output
242

user output
242

Test 39

Verdict: ACCEPTED

input
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

correct output
4287

user output
4287

Test 40

Verdict: ACCEPTED

input
eeeabcbabhd cb ddebchdigaiggfb...

correct output
15

user output
15

Test 41

Verdict: ACCEPTED

input
ghfhehdffdd i icdehheefd hadaf...

correct output
431

user output
431

Test 42

Verdict: ACCEPTED

input
syadhzdgckekirkyow irnflwfanrx...

correct output
8039

user output
8039

Test 43

Verdict: ACCEPTED

input
adcdbbbbbaaccabb ccdccabcc cdb...

correct output
9409

user output
9409

Test 44

Verdict: ACCEPTED

input
aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa...

correct output
168696

user output
168696