hdu 1671 Phone List(字典树)
知道bug的时候我眼泪掉下来。。。
我的第一道字典树,看了字典树的注意事项和实现方式,我写这道题的时候格外认真,就是奔着1A去的。结果这是几A来着?
第一遍写的时候提交MLA,我看了一下,是因为我释放内存的函数写的有问题,‘==’写成了‘=’。修改之后提交wa,这个我就想不明白了,我可是测试了很多组数据的。
之后又尝试性的做了一个小小地修改,再次提交还是wa。然后是各种思考bug,想了好久突然想到我是用数字输入的,这样的话前导0会被忽略掉。
我的1A就这么没了。
#include<stdio.h>
#include<string.h>
#include<malloc.h>
#define N 15
struct node
{
node* a[10];
int flag;
};
node *root;
int InsertTree(char *ss)
{
int k,mark[15];
int ln;
ln=strlen(ss);
k=0;
int i;
for(i=ln-1;i>=0;i--)
mark[k++]=ss[i]-'0';
node *cur=root;
node *s;
int flag=0;
for(i=k-1;i>=0;i--)
{
if(i!=0)
{
if(cur->flag==1)
{
flag=1;
break;
}
if(cur->a[mark[i]]!=NULL&&cur->a[mark[i]]->flag!=1)
cur=cur->a[mark[i]];
else if(cur->a[mark[i]]!=NULL&&cur->a[mark[i]]->flag==1)
{
flag=1;
break;
}
else
{
s=(node *)malloc(sizeof(node));
memset(s->a,0,sizeof(s->a));
cur->a[mark[i]]=s;
s->flag=0;
cur=s;
}
}
else
{
if(cur->a[mark[i]]!=NULL)
{
flag=1;
break;
}
else
{
s=(node *)malloc(sizeof(node));
memset(s->a,0,sizeof(s->a));
cur->a[mark[i]]=s;
s->flag=1;
cur=s;
}
}
}
return flag;
}
void Free(node *root)
{
if(root->flag==1)
free(root);
else
{
int i;
for(i=0;i<=9;i++)
{
if(root->a[i]!=NULL)
Free(root->a[i]);
}
free(root);
}
return ;
}
int main()
{
int T;
scanf("%d",&T);
char s[15];
while(T--)
{
int n;
scanf("%d",&n);
getchar();
root=(node *)malloc(sizeof(node));
memset(root->a,0,sizeof(root->a));
root->flag=0;
int flag=0;
while(n--)
{
int x;
gets(s);
if(InsertTree(s))
flag=1;
}
if(flag)
printf("NO\n");
else
printf("YES\n");
Free(root);
}
return 0;
}
hdu 1671 Phone List(字典树)的更多相关文章
- hdu 1671 Phone List 字典树
// hdu 1671 Phone List 字典树 // // 题目大意: // // 有一些电话号码的字符串长度最多是10,问是否存在字符串是其它字符串的前缀 // // // 解题思路: // ...
- [ACM] hdu 1671 Phone List (字典树)
Phone List Problem Description Given a list of phone numbers, determine if it is consistent in the s ...
- hdu 1671 Phone List 字典树模板
Given a list of phone numbers, determine if it is consistent in the sense that no number is the pref ...
- hdu 1251 统计难题 (字典树入门题)
/******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...
- HDU 5536 Chip Factory 字典树
Chip Factory Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hdu.edu.cn/showproblem.php?pid= ...
- HDU 1298 T9(字典树+dfs)
http://acm.hdu.edu.cn/showproblem.php?pid=1298 题意:模拟手机9键,给出每个单词的使用频率.现在给出按键的顺序,问每次按键后首字是什么(也就是要概率最大的 ...
- HDU 2846 Repository(字典树,每个子串建树,*s的使用)
Repository Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- hdu 1251 统计难题(字典树)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Total Subm ...
- HDU 1298 T9【字典树增加||查询】
任意门:http://acm.hdu.edu.cn/showproblem.php?pid=1298 T9 Time Limit: 2000/1000 MS (Java/Others) Memo ...
- hdu 1251 统计难题 (字典树(Trie)<PS:C++提交不得爆内存>)
统计难题Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submis ...
随机推荐
- 对JSON数组对象排序-有键相同的元素,分组数量不一致,可采用如下的JS进行循环表格输出
var now=eval(data.data); // now.sort(sortBy('bigIdOrder', true, parseInt)); var tab=""; va ...
- 把VBScript的函数迁移到C#.NET
原文:把VBScript的函数迁移到C#.NET VBScript 5.6 Functions C# code Abs System.Math.Abs Array New Object() { } A ...
- WebApiContrib
https://github.com/WebApiContrib ASP.NET Web API and Protocol Buffers Protocol Buffers are a super e ...
- MVC 分页1 标准的url分页
一. 将mvcpager ddl 引用到web服务项目中. 二. 在view加入 <%@ Import Namespace="Webdiyer.WebControls.Mvc" ...
- C# FileSystemWatcher 监视磁盘文件
C# FileSystemWatcher 监视磁盘文件变更 简化需求:有一个简化了的需求是这样的:有一个拍照程序在运行,一旦抓拍之后则将图片文件存储至某目录,然后图片要上传至远程服务器并update数 ...
- 5款新颖的ReSharper插件
ReSharper是著名的代码生成工具.自ReSharper 8.0版本发布以来,新的扩展管理器.模板设置.分发安装等功能改革使得对插件的要求变高.接下来小编整理了5款新颖插件. JetBox 当开发 ...
- leetcode[159] Longest Substring with At Most Two Distinct Characters
找到最多含有两个不同字符的子串的最长长度.例如:eoeabc,最长的是eoe为3,其他都为2. 思路: 用p1,p2表示两种字符串的最后一个出现的下标位置.初始p1为0. p2为-1.start初始化 ...
- oracle实现自加力id
--oracle实现自加力id --创建一个T_StudentInfo表 create table T_StudentInfo ( "id" integer not null pr ...
- 一步一步实现基于Task的Promise库(五)waitFor和waitForAny的实现
在实现waitFor方法之前,我们先要搞明白下面这些问题: 1. waitFor方法的形参有限制吗? 没有!如果形参是Task类型,不应该启动Task,如果是function类型,会执行方法.所以wa ...
- ArcGIS 10.1 发布使用ArcEngine自定义的GP服务
1. 新建立GP模型 在VS2010中新建一个普通的程序及,引入ArcEngine相关的dll.在该DLL中定义一个或多个GP类和一个GP工厂类.GP类要继承IGPFunction2接口,GP工厂类要 ...