|
Time Limit: 1sec memory limit: 64 mbdescription Given a list of phone numbers, determine if it is consistent in the sense that no number is the pre-defined X of another. Let's say the phone catalogue listed these numbers: • Emergency 911 • Alice 97 625 999 • Bob 91 12 54 26 In this case, it's not possible to call Bob, because the central wocould direct your call to the emergency line as soon as you had dialled the specified rst three digits of Bob's phone number. so this list wocould not be consistent. InputThe specified rst line of input gives a single integer, 1 ≤ T ≤ 40, the number of test cases. each test case starts with N, the number of phone numbers, on a separate line, 1 ≤ n ≤ 10000. then follows n lines with one unique phone number on each line. A phone number is a sequence of at most ten digits. OutputFor each test case, output "yes" if the list is consistent, or "no" otherwise. Sample input copy sample input to clipboard2391197625999911254265113123401234401234598346 Sample outputNOYES The following program uses 0.24, the key is that dynamic allocation takes a lot of time, and two pointers are used, the space is not ideal, the solution is to apply for an array, TreeNode data[1000000], Instead of using new in each allocation, you can directly map it to an array. In this way, it takes 0.07 seconds (tested) to greatly improve (this method is indeed good for this question, But how big is the array? Try it yourself. I will be there in 60009. Example) # Include <iostream> # include <string. h> # include <stdlib. h> using namespace STD; Class treenode; typedef treenode * tree; typedef tree position;/* Implementation */class treenode {public: Tree downtree; tree righttree; char digt; treenode () {downtree = righttree = NULL ;};/** usage: int (char * phonenum, tree root) * uses root as the root to initialize a tree, each node of the number assigns a value to digt * If the root is not empty, it is initialized in the right tree of the root */tree & INI (char phonenum [], tree & root) {If (R Oot! = NULL) // return INI (phonenum, root-> righttree); If (phonenum [0] = '/0') // After the tree is created, return nullreturn root; // otherwise, create the next level tree downtreeroot = new treenode; root-> digt = phonenum [0]; INI (& phonenum [1], root-> downtree);}/** use: Find (char item, tree cur) * search for item in the level tree of cur * If found, returns the pointer to the position. Otherwise, null */inline Position & findinsamelevel (char & item, Tree & cur) {If (cur = NULL) // cur is null, this level is not initialized, return a null pointer return cur; do {// otherwise, along the downtree Find if (cur-> digt = item) return cur; else cur = cur-> righttree;} while (cur! = NULL); Return cur;}/** usage: isprefix (char phonenum [], tree root) * determines whether a prefix exists for the number of root nodes. * If yes, true * Otherwise, false */bool isprefix (char phonenum [], const Tree & root) {tree cur = root; bool flag = false; // flag is true if there's prefixfor (INT I = 0; I <strlen (phonenum); I ++) {tree tmpcur = cur; // record cur, if cur is set to null, if (findinsamelevel (phonenum [I], cur) {flag = true; // find a corresponding flag and set it to trueif (findinsameleve L (phonenum [I], cur)-> downtree! = NULL) // found, and the point is not the end {cur = findinsamelevel (phonenum [I], cur)-> downtree; continue;} elsereturn true; // The point is the end, the data in the tree is phonenum prefix} else // if this point cannot be found, initialize a tree {cur = tmpcur; flag = false; INI (& phonenum [I], cur-> righttree ); break; // process the next number after initialization. If (FLAG) return true; elsereturn false;} int main () {int testcase; cin> testcase; while (testcase --) {int numofphonenum; CIN> numofphonenum; tree root; root = NULL; bool flag; flag = true; // true if there is no prefix for (INT I = 0; I <numofphonenum; I ++) {char phonenum [10]; CIN> phonenum; if (I = 0) {INI (phonenum, root); // initialize the first number} elseif (isprefix (phonenum, root) // determine whether the entered number is a prefix {flag = false; char rubbish [10]; for (Int J = 0; j <numofphonenum-i-1; j ++) // forms the prefix and continues to receive the remaining phonenumcin> rubbish; cout <"no" <Endl; break ;}} if (FLAG) // does not constitute, output yescout <"yes" <Endl;} return 0 ;} |