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