sicily 1426 PhoneList 使用动态分配是在是太耗时了
来源:互联网 发布:云计算logo含义 编辑:程序博客网 时间:2024/05/28 11:29
Given a list of phone numbers, determine if it is consistent in the sense that no number is the prefix 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 would direct your call to the emergency line as soon as you had dialled the first three digits of Bob’s phone number. So this list would not be consistent.
The first 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.
For each test case, output “YES” if the list is consistent, or “NO” otherwise.
2391197625999911254265113123401234401234598346
NOYES
下面这个程序用时0.24,关键是动态分配耗时比较多,且使用两个指针,空间也不理想,解决办法为申请一块数组,
TreeNode data[1000000],
每次分配时不用new,而直接映射到数组就好,这样用时为0.07秒(已测试),大大提高(这种方法对于这道题的确很好,
不过数组开多大,你自己试罗,我60009就过
样例如此而已)
#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;}}; /* *用法:int (char* PhoneNum, Tree Root) *以Root为根往下初始化一棵树,数的每个节点以PhoneNum[0]给digt赋值 *若根不为空,则在根的右树初始化 */Tree& ini(char PhoneNum[],Tree& Root){if(Root != NULL)//根不为空return ini(PhoneNum, Root->RightTree);if(PhoneNum[0] == '/0')//树建立完毕,返回NULLreturn Root;//否则,继续建立下一级树DownTreeRoot =new TreeNode;Root->digt = PhoneNum[0];ini(&PhoneNum[1], Root->DownTree);}/* *使用:Find(char item, Tree Cur) *在Cur所在的这一级树进行查找item *若找到,返回所在位置的指针,否则,返回NULL */inline Position& FindInSameLevel(char& item, Tree& Cur){if(Cur == NULL)//Cur为空,该级未初始化,返回空指针return Cur;do{//否则,沿着DownTree查找if(Cur->digt == item)return Cur;else Cur = Cur->RightTree;}while(Cur != NULL);return Cur;}/* *用法:isPrefix(char PhoneNum[], Tree Root) *判断以Root为根的数是否存在前缀现象 *若有,返回True *否则,返回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;//记录Cur,若Cur被定为为NULL,则对其重定位if(FindInSameLevel(PhoneNum[i], Cur)){flag = true;//找到一个对应的就可以把flag设置为trueif(FindInSameLevel(PhoneNum[i], Cur)->DownTree != NULL)//找到,且该点不为末端{Cur = FindInSameLevel(PhoneNum[i], Cur)->DownTree;continue;}elsereturn true;//该点为末端,树中数据为PhoneNum前缀}else//找不到该点,则初始化一棵树{Cur= tmpCur ;flag = false;ini(&PhoneNum[i], Cur->RightTree);break;//初始化完了则继续处理下一个号码,不判断flag}}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 如果没前缀for(int i=0; i< NumofPhoneNum; i++){char PhoneNum[10];cin>>PhoneNum;if(i == 0){ini(PhoneNum, Root);//初始化第一个号码}elseif(isPrefix(PhoneNum, Root)) //判断后面输入的号码是否构成前缀{flag = false;char rubbish[10];for(int j=0; j < NumofPhoneNum - i-1; j++)//构成了前缀,继续接收完剩余的PhoneNumcin>>rubbish;cout<<"NO"<<endl;break;}}if(flag)//不构成,输出YEScout<<"YES"<<endl;}return 0;}
- sicily 1426 PhoneList 使用动态分配是在是太耗时了
- 是我太娇气了
- 是我太天真了吗
- 难道是我太小气了吗?
- 是我太笨了吧
- (随笔) 是我们眼界太狭隘了
- beagle实在是太coool了
- 五百强各大行业简介+面试流程~~~~不留下是在太可惜了
- 这些话也许是你一生都在寻找的,太经典了!!
- 每次写算法程序的时候总是纠结在动态分配内存上面,这次总算是明白了
- 市场营销学经典语句摘录,实在是太经典了!
- 一个好的身体实在是太重要了
- 这个东西实在是太不好用了
- 现在的学生们,真的是太有勇气了
- 放安全套的最佳位置是。。。。。。太XX了
- 昨天飞鸽传书可能是因为太累了
- 我们是太迫于和擅长Google了吧
- UITableView进行讲解 真的是太详细了
- .net 服务
- 为什么需要htons(), ntohl(), ntohs(),htons() 函数
- .net 应用程序
- Linux 系统内核空间与用户空间通信的实现与分析
- 简单的事件通知机制实现
- sicily 1426 PhoneList 使用动态分配是在是太耗时了
- 极限编程概述
- 关于CListCtrl的资源
- linux中断 下半部
- unity3d 破解
- 《C++编程思想》第十章引用和拷贝构造函数(书摘)
- Get Android System Information
- 阅读优秀的代码是提高个人能力的捷径
- 神级排序算法:sleep sort