Phone List
题目背景:
分析:trie树的裸题,直接先全部读入,然后判定是否有前缀存在即可。
Source:
#include
#include
#include
#include
#include
#include
#include
using namespace std; int tot, t, n; string str; struct node { int cnt; node *next[10]; node() { cnt = 0; memset(next, 0, sizeof(next)); } }; node *root, *tree[10010]; inline void R(int &v) { char c = 0; bool p = true; v = 0; while (!isdigit(c)) { if(c == '-') p = false; c = getchar(); } while (isdigit(c)) { v = (v << 3) + (v << 1) + (c ^ '0'); c = getchar(); } if (!p) v = -v; } void getin(string &s) { int len = s.size(); node *p = root; for (int i = 0; i < len; ++i) { if (!p->next[s[i] - '0']) p->next[s[i] - '0'] = new node(); p = p->next[s[i] - '0']; p->cnt++; } tree[tot++] = p; } bool find() { for (int i = 0; i < tot; ++i) if (tree[i]->cnt > 1) return false; return true; } void remove(node *x) { if (!x) return ; for (int i = 0; i < 10; ++i) remove(x->next[i]); delete x; } void work() { cin >> t; while (t--) { root = new node(); tot = 0; cin >> n; for (int i = 1; i <= n; ++i) { cin >> str; getin(str); } if (find()) cout << "YES" << '\n'; else cout << "NO" << '\n'; remove(root); } } int main() { ios::sync_with_stdio(false); cin.tie(NULL); work(); return 0; }