답안 #528976

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
528976 2022-02-21T20:53:27 Z jeremias Type Printer (IOI08_printer) C++14
0 / 100
48 ms 54748 KB
#include <bits/stdc++.h>
#define forn(i, n) for (int i = 0; i < int(n); i++)
#define forsn(i, s, n) for (int i = int(s); i < int(n); i++)
#define all(x) x.begin(), x.end()
#define LSB(x) x & -x
typedef long long ll;
using namespace std;

/**
Trie
traverse in sorted order
**/

int trie[500020][27], d[500020], nxt = 1, rta = 0;
bool isWord[500020], dont[500020];
string longest = "";

void insert(string &s)
{
    int i = 0, v = 0;
    while (i < s.size())
    {
        if (trie[v][(s[i]-'a')] == -1)
        {
            trie[v][(s[i]-'a')] = nxt++;
        }
        v = trie[v][(s[i++]-'a')];
    }
    isWord[v] = true;
}

void depths(int node, int depth)
{
    d[node] = depth;
    forn(i, 26)
    {
        if (trie[node][i] != -1)
        {
            depths(trie[node][i], depth + 1);
        }
    }
}

void dfs(int node)
{
    int d_it = -1;
    forn(i, 26)
    {
        if (trie[node][i] != -1)
        {
            if (!dont[trie[node][i]])
            {
                rta++;
                //cout << (char)(i + 'a') << "\n";
                if(isWord[trie[node][i]]){
                    //cout << "P\n";
                    rta++;
                }
                dfs(trie[node][i]);
                rta++;
                //cout << "-\n";
            }
            else
                d_it = i;
        }
    }
    if (d_it != -1){
        rta++;
        //cout << (char)(d_it + 'a') << "\n";
        if(isWord[trie[node][d_it]]){
            //cout << "P\n";
            rta++;
        }
        dfs(trie[node][d_it]);
    }
}

int main()
{
    iostream::sync_with_stdio(false);
    cin.tie(NULL);
    int n;
    cin >> n;
    memset(trie, -1, sizeof(trie));
    memset(isWord, false, sizeof(isWord));
    memset(dont, false, sizeof(dont));
    forn(i, n)
    {
        string s;
        cin >> s;
        insert(s);
        if (s.size() > longest.size())
        {
            longest = s;
        }
    }
    int v = 0;
    forn(i, longest.size())
    {
        v = trie[v][longest[i] - 'a'];
        dont[v] = true;
    }
    // declare depths
    depths(0, 0);
    // dfs
    dfs(0);
    // display
    cout << rta << "\n";

    return 0;
}

/*
 */

Compilation message

printer.cpp: In function 'void insert(std::string&)':
printer.cpp:21:14: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   21 |     while (i < s.size())
      |            ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Incorrect 24 ms 54092 KB Line "" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 22 ms 54072 KB Line "" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 22 ms 54016 KB Line "" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 21 ms 54092 KB Line "" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 21 ms 54092 KB Line "" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 22 ms 54092 KB Line "" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 24 ms 54220 KB Line "" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 29 ms 54312 KB Line "" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 48 ms 54748 KB Line "" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -
# 결과 실행 시간 메모리 Grader output
1 Incorrect 41 ms 54604 KB Line "" doesn't correspond to pattern "[a-z\-P]{1}"
2 Halted 0 ms 0 KB -