Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- const int k = 26;
- struct Vertex {
- int next[k];
- bool leaf = false;
- Vertex() {
- fill(next,next + k,-1);
- }
- };
- vector<Vertex> trie(1);
- void insert (string const& s) {
- int v = 0;
- for (int i = 0; i < (int) s.size(); ++i) {
- if (trie[v].next[s[i] - '0'] == -1) {
- trie[v].next[s[i] - '0'] = trie.size();
- trie.emplace_back();
- }
- v = trie[v].next[s[i] - '0'];
- }
- trie[v].leaf = true;
- }
- bool find (string const& s) {
- int v = 0;
- for (int i = 0; i < (int) s.size(); ++i) {
- if (trie[v].next[s[i] - '0'] == -1) return false;
- v = trie[v].next[s[i] - '0'];
- if ((int) s.size() == i + 1 and trie[v].leaf) return false;
- }
- return true;
- }
- int main () {
- ios::sync_with_stdio(false);
- cin.tie(nullptr);
- cout.tie(nullptr);
- int T;
- cin >> T;
- for (int test_case = 1; test_case <= T; ++test_case) {
- int n;
- cin >> n;
- vector<pair<int,string>> s;
- for (int i = 0; i < n; ++i) {
- string in;
- cin >> in;
- s.push_back(make_pair((int) in.size(),in));
- }
- sort (s.rbegin(),s.rend());
- bool yes = true;
- for (int i = 0; i < (int) s.size(); ++i) {
- if (find(s[i].second)) {
- yes = false;
- break;
- }
- insert(s[i].second);
- }
- cout << (yes ? "YES\n" : "NO\n");
- }
- //cerr << "Time elapsed :" << clock() * 1000.0 / CLOCKS_PER_SEC << " ms" << '\n';
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement