hdu_1671 - 字典树
来源:互联网 发布:apache centos e325 编辑:程序博客网 时间:2024/06/06 12:25
一道非常简单的字典树,一开始没有写析构函数,结果居然超内存了!!!悲剧。。。
话说哥的写法还是很标准的~~
#include<iostream>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
const int ax='0';
struct tree
{
bool has;
tree *p[10];
tree()
{
for(int i=0;i<10;i++)
{
p[i]=NULL;
}
has=false;
}
~tree()
{
for(int i=0;i<10;i++)
{
if(p[i]!=NULL)
{
delete p[i];
}
}
}
}*head,*now;
int T,w;
string a,b,c;
vector<string>vs;
bool insert(string x)
{
for(int i=0;i<x.length();i++)
{
if(i==0)
{
if(head->p[x[i]-ax]==NULL)
{
head->p[x[i]-ax]=new tree;
now=head->p[x[i]-ax];
}
else
{
now=head->p[x[i]-ax];
if(now->has==true)
{
return false;
}
}
}
else
{
if(now->p[x[i]-ax]==NULL)
{
now->p[x[i]-ax]=new tree;
now=now->p[x[i]-ax];
}
else
{
now=now->p[x[i]-ax];
if(now->has==true)
{
return false;
}
}
}
}
for(int i=0;i<10;i++)
{
if(now->p[i]!=NULL)
{
return false;
}
}
now->has=true;
return true;
}
bool fuck()
{
for(int i=0;i<vs.size();i++)
{
if(!insert(vs[i]))
{
return false;
}
}
return true;
}
int main()
{
cin>>T;
for(int tt=1;tt<=T;tt++)
{
cin>>w;
vs.clear();
head=new tree;
for(int i=1;i<=w;i++)
{
cin>>a;
vs.push_back(a);
}
if(fuck())
{
cout<<"YES"<<endl;
}
else
{
cout<<"NO"<<endl;
}
delete head;
}
return 0;
}
- hdu_1671 - 字典树
- Phone List (HDU_1671) 字典树
- hdu_1671 Phone List(字典树)
- HDU_1671 POJ_3630 Phone List 【字典树】
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树。。
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树
- java 中文乱码
- Memcache and Mongodb
- Oracle建表,建序列,建触发器,自动生成唯一主键
- 2011-10-28 jsp include <jsp:useBean>
- JQuery合并某列(行)单元格(绿色环保低碳全自动)---免费试用.你!!!还等什么?
- hdu_1671 - 字典树
- Lucene学习总结之三:Lucene的索引文件格式(1)
- WPF数据绑定详解
- 理解重要性采样
- linux常用头文件详解(转)
- ASP.NET MVC3学习总结(一)ASP.NET MVC介绍
- SQL Server 2000安装时碰到的问题
- 聊胜于无 Java之Caesar与Vigenere实现
- SASE+:一种使用克林闭包应用于事件流的敏捷语言