Hangzhou Electric Acm1671--phone list~~ dictionary tree

Source: Internet
Author: User

This is also a simple application of the dictionary tree, but it is not a letter, but a number.

The question is whether the input string will be prefixed with another string. It's that simple.

The following is the code for AC:

#include <iostream> #include <cstring>using namespace std;class node//node structure {public:node* p[1 0];};                 node* Root;                    Define root node int ans; leaf node void Insert (char* str)//insert operation function {int len = strlen (str); node *q, *p;q = root;for (int i = 0; i < len; i++) {in T id = str[i]-' 0 '; if (q->p[id] = = NULL)//The position does not exist, new one {P = new node;for (int j = 0; J <; J + +) P->p[j] = N Ull;q->p[id] = P;q = Q->p[id];} else//exists, directly equal to his successor q = Q->p[id];}} void Fun (node *a)//recursive find how many leaf nodes {int flag = 0;for (int i = 0; i <; i++) {if (A->p[i]! = NULL) {flag = 1;break;}} if (!flag)//The node is a leaf node, Ans++{ans++;return;} for (int j = 0; J <; J + +) if (a->p[j]! = NULL) Fun (A->p[j]);} void Dele (node *a)//delete operation function, also recursive completion {for (int i = 0; i <; i++) if (a->p[i]! = NULL) dele (A->p[i]);d El Ete A;} int main () {//freopen ("Data.txt", "R", stdin); int T, N;char str[15];cin >> t; while (t--) {root = new Node;ans = 0;for (int j = 0; J <; J + +)//Initialize pointer array for root node proot->p[j] = null;cin >> n;fo               R (int i = 0; i < n; i++) {cin >> Str;insert (str);                      Insert}fun (root);                            Count the number of leaf nodes if (ans = = N)//leaf node equals the number of inputs n, then yescout << "YES" << endl;else                     Otherwise nocout << "NO" << endl;dele (root); Delete}return 0;}


Hangzhou Electric Acm1671--phone list~~ dictionary tree

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.