刚学的字典树,代码写得很不熟练。写法上也没有什么特别的优化,就是以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. [C#参考]事件和委托的关系

    前面说了委托,接下来就要说一说事件了,同时最后再说一下委托和事件的区别. 事件和委托很相似,事件就好像是被简化的针对特殊用途的委托.看下面的图: 从这张图中能看到,事件是发布者的一个成员,它不是类型. ...

  2. eclipse修改默认工作空间

    新安装的myEclipse(eclipse)第一次启动时就会弹出让你选择工作空间的对话框 如果勾选了Use this as the default and do not ask again 下次要启动 ...

  3. mysql存储过程详解[转]

    1.      存储过程简介   我们常用的操作数据库语言SQL语句在执行的时候需要要先编译,然后执行,而存储过程(Stored Procedure)是一组为了完成特定功能的SQL语句集,经编译后存储 ...

  4. Amazon Hiring Campus 2013 - Final 6

    Let's assume that there is a simple market for beans. Every day there is a published bean price in t ...

  5. 雪佛兰Suburban_百度百科

    雪佛兰Suburban_百度百科 雪佛兰Suburban

  6. JS获取中文拼音首字母,并通过拼音首字母高速查找页面内的中文内容

    实现效果: 图一: 图二: watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvdGVzdGNzX2Ru/font/5a6L5L2T/fontsize/400/f ...

  7. Win7安装IIS

    非常明显的,我们做系统是用来给人用的,所以这就涉及到对系统的公布问题.仅仅有公布之后.别人才干通过訪问你的IP和port号来訪问你的程序. 而系统公布一般都是在IIS上面进行系统公布,所以我们就必需要 ...

  8. Qt程式异常崩溃处理技巧(Win)

    这篇文章谈的是 Qt4 程式在视窗系统下的异常崩溃处理技巧.所以须要在头文件里包括"#include <Windows.h>". 首先,程式难免会有异常崩溃的时候.重要 ...

  9. 【Hibernate】双向多对多Set查询

    一个计划对于多个竞价,一个竞价对应多个计划. 1.实体 /** * @author Tidy * @Description 计划 */ public class EbgStockPlanContent ...

  10. arduino电子琴(2015-11-04)

    前言 这是论坛上一个坛友问的问题,想做一个可变音调的电子琴,想着正好练练手,就顺手做一下. 接线图