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 ...
随机推荐
- TCP粘包和半包的处理方法
先把处理的方法的代码放这里: 解析数据帧的代码: bool CSocket::findData(byte* buff, int& len) { for (int i = 0; i <= ...
- django开发简易博客(一)
这几篇博客是根据GoodSpeed的博客该写的,看了他的博客收获很大,但是他的博客从第三篇开始,条理很不清晰,加之又是几年之前写的,编写环境发生很大改变,所以对他的博客进行了一个整理,加入了一些自己的 ...
- YII2 使用js
1.在 /backend/assets/ 中新建一个文件 CollectionAsset.php <?php /** * @link http://www.yiiframework.com/ * ...
- VS2010/MFC对话框一:创建对话框模板和修改对话框属性
创建对话框主要分两大步: 第一,创建对话框资源,主要包括创建新的对话框模板.设置对话框属性和为对话框添加各种控件: 第二,生成对话框类,主要包括新建对话框类.添加控件变量和控件的消息处理函数等. 创建 ...
- 小程序员在android移动应用上的赚钱经历
先说说我自己吧,二线城市(以外包为主)的小程序员,工作多年了,月收入5-6K.主要从事asp.net web网站开发,java,c++,php,ruby都懂一些,属于那种对问题不求甚解型,爱好电脑游戏 ...
- 你喜欢SOAP吗?反正我不喜欢!
叫什么Simple Object Access Protocol,实际上一点都不Simple! 说什么轻量级协议,从它基于XML的编码就知道它有多臃肿! 说什么跨平台特性,其实各个语言需要自己实现一整 ...
- gmake使用注意
今天使用gmake把sgml变成html的时候出现了无法找到gmake的困惑(本人linux为ubuntu15.04),上网搜索发现: gmake是GNU Make的缩写.Linux系统环境下的mak ...
- BZOJ 3391: [Usaco2004 Dec]Tree Cutting网络破坏( dfs )
因为是棵树 , 所以直接 dfs 就好了... ---------------------------------------------------------------------------- ...
- Android空指针异常的常见情况
把我经常遇到的nullpoitException写在这里,以便以后自己查找原因. 1.用findViewById(param )的方法获取一个view对象的时候,有的时候其实应该是获取一个layout ...
- Python之路Day6
Day6的主要内容是: configparser模块 shutil模块 subprocess模块 处理xml的模块 1.configparser模块 #! /usr/bin/env python # ...