# | Time | Username | Problem | Language | Result | Execution time | Memory |
---|---|---|---|---|---|---|---|
919496 | silverfox | Vlak (COCI20_vlak) | C++14 | 16 ms | 22872 KiB |
This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#define TEST "filename"
using namespace std;
const int maxn = 200000;
class Trie {
struct Node {
int child[26];
int exist, cnt;
};
int cur = 0;
Node nodes[maxn+1];
public:
Trie() {
memset(nodes[0].child, -1, sizeof(nodes[0].child));
nodes[0].exist = nodes[0].cnt = 0;
}
int newNode() {
cur++;
memset(nodes[cur].child, -1, sizeof(nodes[cur].child));
nodes[cur].exist = nodes[cur].cnt = 0;
return cur;
}
void addString(string s) {
int pos = 0;
for (char c: s) {
int f = c - 'a';
if (nodes[pos].child[f] == -1) {
nodes[pos].child[f] = newNode();
}
pos = nodes[pos].child[f];
nodes[pos].cnt++;
}
nodes[pos].exist++;
}
int maxPrefix(string s) {
int pos = 0;
int len = 0;
for (char c: s) {
int f = c - 'a';
if (nodes[pos].child[f] == -1) { //prefix doesnt match anymore
return len;
}
pos = nodes[pos].child[f];
len++;
}
return len;
}
bool findLonger(string s) {
int pos = 0;
for (char c: s) {
int f = c - 'a';
pos = nodes[pos].child[f];
}
for (int i = 0; i < 26; i++) {
if (nodes[pos].child[i] != -1) { return true; }
}
return false;
}
bool letterAfterPrefix(string s) {
int pos = 0;
for (char c: s) {
int f = c - 'a';
if (nodes[pos].child[f] == -1) { //prefix doesnt match anymore
break;
}
pos = nodes[pos].child[f];
}
for (int i = 0; i < 26; i++) {
if (nodes[pos].child[i] != -1) { return true; }
}
return false;
}
bool ninaWin(string s) {
int maxPref = this->maxPrefix(s);
if (maxPref == s.size()) {
if (this->findLonger(s)) { return false; } //B longer than A
else {return s.size() % 2 == 1;} //B same length as A
}
else if (maxPref < s.size()) {
if (this->letterAfterPrefix(s)) {return s.size() % 2 == 0; } //B and A share some prefix
else { return true; } //B shorter than A
}
}
};
int main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
if (false) {
freopen(TEST".in", "r", stdin);
freopen(TEST".out", "w", stdout);
}
int n; cin >> n;
string a[n]; for (int i = 0; i < n; i++) {cin >> a[i];}
int m; cin >> m;
Trie trie;
for (int i = 0; i < m; i++) {
string s; cin >> s;
trie.addString(s);
}
for (int i = 0; i < n; i++) {
if (trie.ninaWin(a[i])) {cout << "Nina"; return 0;}
}
cout << "Emilija";
}
Compilation message (stderr)
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|---|---|---|---|
Fetching results... |