HDU1671用指针实现的字典树
来源:互联网 发布:电信运营商数据 编辑:程序博客网 时间:2024/04/30 13:31
用指针传递字典树,时空效率更高
用字符串代替字符数组,更方便
用字符串代替字符数组,更方便
#include <iostream>#include <string>#include <algorithm>using namespace std;string str[10005];struct dictree{struct dictree *child[10];int n;dictree(){ memset(child,0,sizeof(child));n=0;}};bool insert(dictree* root,string s){int len,i,j;struct dictree *current,*newnode;len=s.length();if(len==0)return 0;current=root;for(i=0;i<len;i++){if(current->n == 1)return 1;if(current->child[s[i]-'0']!=0)current=current->child[s[i]-'0'];else{newnode=(struct dictree *)malloc(sizeof(struct dictree));for(j=0;j<10;j++)newnode->child[j]=0;current->child[s[i]-'0']=newnode;current=newnode;current->n=0;}if(i == len - 1)current->n=1;}return 0;}bool cmp(string s1, string s2){return s1.length() < s2.length();}void clear(dictree* root){if(root == NULL)return;for(int i = 0; i < 10; i++)clear(root->child[i]);delete root;}int main(){int t, n, i;cin>>t;while(t--){dictree *root = new dictree;bool flag = 0;cin>>n;for(i = 0; i < n; i++)cin>>str[i];sort(str, str+n, cmp);for(i = 0; i < n; i++){if(insert(root, str[i])){flag = 1;break;}}if(flag == 1)cout<<"NO"<<endl;elsecout<<"YES"<<endl;clear(root);}return 0;}
- HDU1671用指针实现的字典树
- HDU1671字典树
- hdu1671之字典树
- hdu1671( 字典树)
- hdu1671 字典树 入门
- 字典树HDU1671
- hdu1671 字典树
- 字典树(hdu1671)
- HDU1671 字典树
- HDU1671(字典树)
- hdu1671 字典树
- HDU1671 字典树
- 【字典树】 hdu1671 Phone List
- hdu1671字典树+动态分配内存
- Phone List(hdu1671字典树)
- hdu1671字典树入门题
- hdu1671 Phone List (字典树)
- hdu1671 PhoneList (字典树)
- 经常被vs2008的xx.aspx.designer.cs与界面不同步的问题烦恼
- P欺诈,使用Winpcap对数据包进行拦截
- HDU1394用线段树求逆序数
- 工作中的不顺心
- 使用计算总内存大小的方法确定jvm中为对象分配的存储空间大小
- HDU1671用指针实现的字典树
- asp.net mvc3 css/js压缩合并---持续更新
- zend framework modules
- asp.net下载文件几种方式
- eclipse alt+/ Content Assist问题
- HDU1787欧拉公式之在线算法
- ArrayList
- The OAuth 2.0 Authorization Protocol (第一章,基础概念)
- Win7系统加密