刚学的字典树,代码写得很不熟练。写法上也没有什么特别的优化,就是以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. 基础算法-查找:线性索引查找(I)

    前面介绍的几种查找的算法都是基于数据有序的基础上进行的.但是在实际的应用中,很多数据集可能有惊人的数据量,面对这些海量的数据,要保证记录全部按照当中的某个关键字有序,其时间代价是非常昂贵的,所以这种数 ...

  2. seajs + easyui [转]

    * *content seajs+easyui使用 */ /** * 首先来看看在seajs中jquery和jquery插件如何使用 */ 1.jquery.js define(function(re ...

  3. C语言选择法排序

    #include <stdio.h> int main() { int i, j, p, n, q; ] = {, , , , }; //对无序数组进行排序 ; i<; i++) { ...

  4. angularJS友好URL实现 good

    nginx部署 angularjs时的rewrite问题 使用h5+angularjs完成了一个项目 此项目在正式环境上使用nginx做webserver 此项目的入口在微信/微博分享中 由于分享时的 ...

  5. 禁用 Windows Azure 网站中的 ARR 实例关联

    编辑人员注释: 本博客文章由 Windows Azure 网站团队的项目经理 Erez Benari 撰写. 在 Windows Azure 网站中设置网站的多个实例是横向扩展网站的绝佳方式,Azur ...

  6. UVA 10057 A mid-summer night's dream. 仲夏夜之梦 求中位数

    题意:求中位数,以及能成为中位数的数的个数,以及选择不同中位数中间的可能性. 也就是说当数组个数为奇数时,中位数就只有一个,中间那个以及中位数相等的数都能成为中位数,选择的中位数就只有一种可能:如果为 ...

  7. EntityFramework经典的left join语法

    /*  * 常常看到有人问linq语法怎样写left join的查询语句,但网上找到的都是简单的两表连接.參考意义有限.  * 今天最终项目里要用到复杂的多表连接,同一时候含有多个左连接,  * 恰好 ...

  8. android一个弹出菜单的动画(二)

    假设做一个弹出的控件,我们能够进行加入view: 写class SatelliteMenu extends FrameLayout private void init(Context context, ...

  9. MVC action返回partialView前台html 拼接

    //后台 [HttpPost] public ActionResult GetNextLazyLoadProduct(int[] productIdList)         {           ...

  10. 倒计时IE10+

    直接上代码,dome 里边有我做的列表倒计时(多个同时倒计时)下面是我做的例子,颜色可以自己设置的 <p name="daojishi" style="width: ...