hdu 1075 What Are You Talking About(字典树)
刚学的字典树,代码写得很不熟练。写法上也没有什么特别的优化,就是以1A为第一目标!
可惜还是失败了。
少考虑了一种情况,就是一个单词是另一个单词前缀的问题,写了好久,还是没有1A。不过感觉对字典树有了更深刻的理解。
代码写得不好,看看别人都是怎么写字典树的去。
#include<stdio.h>
#include<string.h>
#include<malloc.h>
struct node
{
node *a[27];
char s[15];
};
char s1[15],s2[15];
char s[3005],ss[15];
int k;
node *root;
void InsertTree()
{
node *cur;
node *s;
int i,ln;
ln=strlen(s2);
cur=root;
for(i=0;i<ln;i++)
{
if(i==ln-1)
{
if(cur->a[s2[i]-'a']!=NULL)
{
strcpy(cur->a[s2[i]-'a']->s,s1);
return ;
}
else
{
s=(node *)malloc(sizeof(node));
memset(s->a,0,sizeof(s->a));
s->s[0]='\0';
cur->a[s2[i]-'a']=s;
strcpy(s->s,s1);
}
}
else if(cur->a[s2[i]-'a']!=NULL)
cur=cur->a[s2[i]-'a'];
else
{
s=(node *)malloc(sizeof(node));
memset(s->a,0,sizeof(s->a));
s->s[0]='\0';
cur->a[s2[i]-'a']=s;
cur=s;
}
}
return ;
}
int FindTree()
{
int i,ln;
node *cur;
cur=root;
ln=strlen(ss);
for(i=0;i<ln;i++)
{
if(i==ln-1)
{
if(cur->a[ss[i]-'a']!=NULL&&cur->a[ss[i]-'a']->s[0]!='\0')
{
printf("%s",cur->a[ss[i]-'a']->s);
return 1;
}
else
return 0;
}
else
{
if(cur->a[ss[i]-'a']!=NULL)
cur=cur->a[ss[i]-'a'];
else
return 0;
}
}
return 0;
}
int main()
{
root=(node *)malloc(sizeof(node));
memset(root->a,0,sizeof(root->a));
while(scanf("%s",s1))
{
if(strcmp(s1,"START")==0)
continue;
else if(strcmp(s1,"END")==0)
break;
scanf("%s",s2);
InsertTree();
}
getchar();
while(gets(s))
{
if(strcmp(s,"START")==0)
continue;
else if(strcmp(s,"END")==0)
break;
int i;
k=0;
for(i=0;s[i]!='\0';i++)
{
if(s[i]>='a'&&s[i]<='z')
{
ss[k++]=s[i];
continue;
}
else if(k!=0)
{
ss[k]='\0';
if(!FindTree())
printf("%s",ss);
memset(ss,0,sizeof(ss));
k=0;
}
printf("%c",s[i]);
}
if(k!=0)
{
ss[k]='\0';
if(!FindTree())
printf("%s",ss);
}
printf("\n");
}
return 0;
}
hdu 1075 What Are You Talking About(字典树)的更多相关文章
- hdu 1075 What Are You Talking About 字典树模板
What Are You Talking About Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/204800 K ...
- HDU 4825 Xor Sum(经典01字典树+贪心)
Xor Sum Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others) Total ...
- HDU 4825 Xor Sum(二进制的字典树,数组模拟)
题目 //居然可以用字典树...//用cin,cout等输入输出会超时 //这是从别处复制来的 #include<cstdio> #include<algorithm> #in ...
- hdu 6191--Query on A Tree(持久化字典树)
题目链接 Problem Description Monkey A lives on a tree, he always plays on this tree. One day, monkey A l ...
- HDU 1251 统计难题 (裸的字典树)
题目链接 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本 ...
- hdu 1004 Let the Balloon Rise(字典树)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1004 Let the Balloon Rise Time Limit: 2000/1000 MS (J ...
- HDU 6191 Query on A Tree(字典树+离线)
Query on A Tree Time Limit: 20000/10000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Othe ...
- HDU 1800 Flying to the Mars 字典树,STL中的map ,哈希树
http://acm.hdu.edu.cn/showproblem.php?pid=1800 字典树 #include<iostream> #include<string.h> ...
- hdu 1075:What Are You Talking About(字典树,经典题,字典翻译)
What Are You Talking About Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 102400/204800 K ...
随机推荐
- BZOJ 1567: [JSOI2008]Blue Mary的战役地图( 二分答案 + hash )
二分答案, 然后用哈希去判断... ------------------------------------------------------------------------- #include ...
- Clob对象转为字符串
项目中遇到一个问题,对方公司把打印好的报表数据存到数据库中,实际上就是把html存在Oracle中,然后需要我们在社保系统里进行查询. 但是他们把数据存放在B数据库,而我们的社保系统用的数据库是B.A ...
- U-Boot在FL2440上移植(四)----支持网卡DM9000和烧写yaffs文件系统
<一>支持网卡芯片DM9000 在driver下,有网卡驱动DM9000x.c 和 DM9000x.h DM9000接在BANK4,位宽16 在include/configs/TX2440 ...
- ioctl函数详细说明
本函数影响由fd 参数引用的一个打开的文件. #include<unistd.h> int ioctl( int fd, int request, .../* void *arg */ ) ...
- FluentConsole是一个托管在github的C#开源组件
FluentConsole是一个托管在github的C#开源组件 阅读目录 1.控制台能有啥滑头? 2.FluentConsole基本介绍 3.使用介绍 4.资源 从该系列的第一篇文章 .NET平台开 ...
- java 语法 java没学好,休想学好安卓!
int...a 里面的...表示可变参数,也就是说这是一个长度不定的数组 instanceof : instanceof关键字用于判断一个引用类型变量所指向的对象是否是一个类(或接口.抽象类.父类)的 ...
- 安装好maven后,在cmd中运行mvn报一下的错误
当然报错,你这个路径下并没有pom.xml文件.你可以运行这个命令: mvn -version.
- hdu 4784 Dinner Coming Soon
spfa+优先队列.刚开始只用的spfa,结果tle到死.然后听队友说要用到优先队列,想了想,对时间分层的话的确每一个结点都只进队列一次即可,因为只有大时间才能更新出小时间,然后就wa成shi了.按队 ...
- 循环调用修正sic86
create or replace procedure rebuild_sic86_wyl(pi_aac001 in number, po_fhz out varchar2, po_msg out v ...
- javascript 入门之简单换肤效果
大家好,我是小强老师,这里简单入门 做一个换肤效果 效果如图所示: 这个案例思路分为两部分: 获取元素对象. var pic1 = document.getElementById('pic1'); v ...