Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //TRIE Data Structure: Check if prefix exist.
- #include <bits/stdc++.h>
- using namespace std;
- #define Size 150005
- class Trie {
- bool found;
- Trie *child[10];
- public:
- Trie() {
- found = false;
- for (int i = 0; i < 10; i++)
- child[i] = NULL;
- }
- void delete_trie() {
- for (int i = 0; i < 10; i++)
- if (child[i] != NULL) {
- child[i]->delete_trie();
- delete child[i];
- }
- }
- bool insert_word(string &word, int pos) {
- if (found){
- return false;
- }
- if (pos == (int)word.length()){
- return found = true;
- }
- int c = (int)(word[pos] - '0');
- if (child[c] == NULL) {
- child[c] = new Trie();
- }
- return child[c]->insert_word(word, ++pos);
- }
- }*root;
- int N;
- string numbers[10005];
- bool cmp(string a,string b){
- if(a.length()<b.length()) return true;
- if(a.length()>b.length()) return false;
- if(a < b) return true;
- return false;
- }
- int main() {
- int nCase;
- cin >> nCase;
- for (int cs = 1; cs <= nCase; cs++) {
- cin >> N;
- root = new Trie();
- for (int i = 0; i < N; i++) {
- cin >> numbers[i];
- }
- sort(numbers,numbers+N,cmp);
- bool f = true;
- for (int i = 0; i < N; i++) {
- f = root->insert_word(numbers[i],0);
- if(f == false) break;
- }
- if (f == true) {
- printf("Case %d: YES\n",cs);
- } else {
- printf("Case %d: NO\n",cs);
- }
- root->delete_trie();
- delete root;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment