刚学的字典树,代码写得很不熟练。写法上也没有什么特别的优化,就是以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(字典树)的更多相关文章

  1. hdu 1075 What Are You Talking About 字典树模板

    What Are You Talking About Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K ...

  2. HDU 4825 Xor Sum(经典01字典树+贪心)

    Xor Sum Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Others) Total ...

  3. HDU 4825 Xor Sum(二进制的字典树,数组模拟)

    题目 //居然可以用字典树...//用cin,cout等输入输出会超时 //这是从别处复制来的 #include<cstdio> #include<algorithm> #in ...

  4. 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 ...

  5. HDU 1251 统计难题 (裸的字典树)

    题目链接 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本 ...

  6. 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 ...

  7. HDU 6191 Query on A Tree(字典树+离线)

    Query on A Tree Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 132768/132768 K (Java/Othe ...

  8. HDU 1800 Flying to the Mars 字典树,STL中的map ,哈希树

    http://acm.hdu.edu.cn/showproblem.php?pid=1800 字典树 #include<iostream> #include<string.h> ...

  9. hdu 1075:What Are You Talking About(字典树,经典题,字典翻译)

    What Are You Talking About Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 102400/204800 K ...

随机推荐

  1. [置顶] 程序员必知(二):位图(bitmap)

    位图是什么? 位图就是数组,一般来说是bit型的数组,具有快速定位某个值的功能,这种思想有很广泛的应用,比如下边两题: 1 找出一个不在5TB个整数中存在的数 假设整数是32位的,总共有4GB个数,我 ...

  2. PHP 时间和日期 总结

    PHP 时间戳 UNIX 时间戳(timestamp)是 PHP 中关于时间日期一个很重要的概念,它表示从 1970年1月1日 00:00:00 到当前时间的秒数之和. 可以使用time()函数来获取 ...

  3. VMWare11虚拟机安装OSX10.9系统资源下载及问题解决

    适配VMware11的MacOSX补丁: http://pan.baidu.com/s/1bnqgtDd 使用方法:将补丁解压到一个完全没有中文的目录下,以管理员方式运行目录中的win-install ...

  4. 运用Python语言编写获取Linux基本系统信息(三):Python与数据库编程,把获取的信息存入数据库

    运用Python语言编写获取Linux基本系统信息(三):Python与数据库编程 有关前两篇的链接: 运用Python语言编写获取Linux基本系统信息(一):获得Linux版本.内核.当前时间 运 ...

  5. QMessageBox 用法

    案例一:QMessageBox msgBox;msgBox.setText("The document has been modified.");msgBox.setInforma ...

  6. repeater一个简单的用法例子

    (前台) <asp:Repeater ID="Repeater1" runat="server"       onitemdatabound=" ...

  7. ASP.NET 内置对象涉略

    一.ASP.NET中内置的常用对象的介绍 本文列举了ASP.NET 的八个内置对象,其中前五个是比较常用的. 1.Response Response 对象用于从服务器向用户发送输出的结果. Write ...

  8. NGUI HUDText

    今天使用HUDText的时候,发现须要如今场景里创建一个UI2dRoot;不然位置不对 或许应该创建一个prefab这样每一个场景都加入一个就可以. using UnityEngine;       ...

  9. Xcode使用source control 时提示the server certificate failed to verify 的解决办法

    wusipingdeMacBook-Pro:~ railgun$ wusipingdeMacBook-Pro:~ railgun$ svn ls https://13.13.13.134:8443/s ...

  10. CentOS 6 用SVN自动提交文件到web服务器

    关于 svn 的安装 参考:[转]Linux(centOS6.5)下SVN的安装.配置及开机启动 经过两天的各种尝试总算解决了,总结如下: 1.在建立库时注意 要让库的名称和  要同步的 web目录名 ...