답안 #1111806

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
1111806 2024-11-13T01:45:58 Z Andwerp Vlak (COCI20_vlak) C++14
70 / 70
25 ms 22608 KB
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<bool> vb;
typedef vector<ld> vd;
typedef vector<vector<int>> vvi;
typedef vector<vector<ll>> vvl;
typedef vector<vector<bool>> vvb;
typedef vector<vector<ld>> vvd;
// typedef __int128 lll;
// typedef __float128 lld;

struct Trie {
    struct TrieNode {
        TrieNode* c[26];
        bool isWord = false;    //true if a word ends on this node
        int numWords = 0;   //counts how many words use this node as prefix
    };

    TrieNode* root;

    Trie() {
        this->root = new TrieNode();
    }

    void insert(string s){
        TrieNode* ptr = root;
        for(int i = 0; i < s.size(); i++){
            ptr->numWords ++;
            int ch = s[i] - 'a';
            if(ptr->c[ch] == nullptr) {
                ptr->c[ch] = new TrieNode();
            }
            ptr = ptr->c[ch];
        }
        ptr->numWords ++;
        ptr->isWord = true;
    }

    TrieNode* query(string s) {
        TrieNode* ptr = root;
        for(int i = 0; i < s.size(); i++){
            int ch = s[i] - 'a';
            ptr = ptr->c[ch];
            if(ptr == nullptr) {
                break;
            }
        }
        return ptr;
    }
};

bool can_win(Trie::TrieNode* r1, Trie::TrieNode* r2) {
    //if r1 has something that r2 does not, then can win
    bool ans = false;
    for(int i = 0; i < 26; i++){
        if(r1->c[i] == nullptr){
            continue;
        }
        if(r2->c[i] == nullptr){
            ans = true;
        }
        ans = ans || (!can_win(r2->c[i], r1->c[i]));
    }
    return ans;
}

signed main() {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL); cout.tie(NULL);
    
    int n;
    cin >> n;
    Trie t1, t2;
    for(int i = 0; i < n; i++){
        string s;
        cin >> s;
        t1.insert(s);
    }
    cin >> n;
    for(int i = 0; i < n; i++){
        string s;
        cin >> s;
        t2.insert(s);
    }
    bool nina_win = can_win(t1.root, t2.root);
    cout << (nina_win? "Nina" : "Emilija") << "\n";
    
    return 0;
}

Compilation message

Main.cpp: In member function 'void Trie::insert(std::string)':
Main.cpp:33:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   33 |         for(int i = 0; i < s.size(); i++){
      |                        ~~^~~~~~~~~~
Main.cpp: In member function 'Trie::TrieNode* Trie::query(std::string)':
Main.cpp:47:26: warning: comparison of integer expressions of different signedness: 'int' and 'std::__cxx11::basic_string<char>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   47 |         for(int i = 0; i < s.size(); i++){
      |                        ~~^~~~~~~~~~
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 592 KB Output is correct
2 Correct 1 ms 592 KB Output is correct
3 Correct 1 ms 592 KB Output is correct
4 Correct 1 ms 512 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 592 KB Output is correct
2 Correct 1 ms 592 KB Output is correct
3 Correct 1 ms 760 KB Output is correct
4 Correct 1 ms 592 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 592 KB Output is correct
2 Correct 1 ms 592 KB Output is correct
3 Correct 1 ms 460 KB Output is correct
4 Correct 1 ms 592 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 1 ms 592 KB Output is correct
2 Correct 1 ms 592 KB Output is correct
3 Correct 1 ms 604 KB Output is correct
4 Correct 1 ms 592 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 18 ms 21328 KB Output is correct
2 Correct 19 ms 19960 KB Output is correct
3 Correct 25 ms 18900 KB Output is correct
4 Correct 20 ms 20560 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 21584 KB Output is correct
2 Correct 19 ms 22608 KB Output is correct
3 Correct 23 ms 20816 KB Output is correct
4 Correct 18 ms 21380 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 19 ms 20316 KB Output is correct
2 Correct 17 ms 20048 KB Output is correct
3 Correct 17 ms 20304 KB Output is correct
4 Correct 20 ms 21660 KB Output is correct