zoj 1109 zoj 1109 Language of FatMouse(字典树)
好开心,手动自己按照字典树的思想用c写了一个优化后的trie字典树,就是用链表来代替26个大小的字符数组。完全按照自己按照自己的想法打的,没有参考如何被人的代码。调试了一天,居然最后错在一个小问题上,郁闷啊!!!最后终于调试出来了!!一次提交就ac了!!开心溢于言表啊!!!!!!
/*
trie树:
插入:
插入一段字符串:
每个字符作为树的一层(同一层的节点通过兄弟节点项连),每个节点如果有后继节点的话,就把最大的后继放到其后面。
如果字符串结束,就在该节点处,connt++;然后用一个字符串指针指向实际匹配的字符串的地址。
查询:
给定一个字符串,通过字符串和树遍历匹配,匹配到该字符串最后的一个节点时,就输出,字符串指针所指向的位置。
*/
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define MAX 100010
typedef struct T
{
int count;/*记录相同字符串的个数*/
struct T * Next;/*下一层排名第1节点的地址*/
struct T * borther;/*同一层比该节点排名后的兄弟*/
char *real;/*实际指向的字符串*/
char msg;/*该节点的信息*/
}Node;
typedef struct N
{
char key[],source[];
}Element;
Element element[MAX];
void create(Node *trie)
{
trie->count=;
trie->Next=trie->borther=NULL;
trie->real=NULL;
}
void insert(Node *trie,int L)
{
int i,j,flag=,h=;
Node *p,*s,*o,*x,*temp;
s=p=trie;
for(i=;i<strlen(element[L].key);i++)
{
if(!s->Next)/*如果上一层下面是空的,就把创建一个将当前的方进*/
{
o=(Node *)malloc(sizeof(Node));
o->msg=element[L].key[i];/*该字符放入其中*/o->count=;
o->Next=o->borther=NULL;o->real=NULL;
s->Next=p=o;;/*将当前节点更新为上一个节点*/
}
else{
h=;
x=p=s->Next;
while(p)
{ if(element[L].key[i]==p->msg){/*如果相同,那么肯定之前存在过,就找下一层*/
h=;break;
}
else if(element[L].key[i]>p->msg){/*如果排名在他前面就停止移动*/
x=p;
p=p->borther;/*如果排名比他后,往后移*/
}
else {
if(p==s->Next)
{
h=;
o=(Node *)malloc(sizeof(Node));
o->msg=element[L].key[i];/*该字符放入其中*/o->count=;
o->Next=NULL;o->real=NULL;
s->Next=o;o->borther=p;p=o;/*将当前节点更新为上一个节点*/
}
break;
}
}
if(h==)
{
o=(Node *)malloc(sizeof(Node));
o->msg=element[L].key[i];/*该字符放入其中*/o->count=;
o->Next=NULL;o->real=NULL;
x->borther=o;o->borther=p;p=o;/*将当前节点更新为上一个节点*/
}
}
if(element[L].key[i+]=='\0'){/*找到字符串的终点就返回*/
p->real=element[L].source;p->count++;
// printf("%s\n",source);
return ;
}
s=p;
}
}
int serch(Node *trie,char *s)
{
int i=;
Node *p=trie->Next;
while(p)
{
if(p->msg==s[i])
{
i++;
if(s[i]=='\0') {
printf("%s\n",p->real);return ;
}
else
{
p=p->Next;
}
}
else
p=p->borther;
}
return ;
}
int main(void)
{
int i,j;
Node trie;/*创建一个树的根节点,没用动态创建*/ char s[*MAX];
create(&trie);/*传递地址,初始化树的根节点的数据*/
j=;
while(gets(s)){
if(strcmp(s,"\0")==)/*输入空行直接跳出*/
break;
for(i=;i<strlen(s);i++)/*输入字符串,将其拆分成键值对字符串*/
if(s[i]==' '){
s[i]='\0';
strcpy(element[j].source,s);
strcpy(element[j].key,&s[i+]);
break;
}
insert(&trie,j);/*将值插入到树中*/
j++;
}
s[]='y';
while(gets(s)){/*输入关键字进行查询*/
if(strcmp(s,"\0")==)/*输入空行直接跳出*/
break;
if(!serch(&trie,s))/*查找失败就输出eh,成功就自动输出*/
printf("eh\n");
s[]='y';
}
return ;
}
zoj 1109 zoj 1109 Language of FatMouse(字典树)的更多相关文章
- zoj 1109 Language of FatMouse(字典树)
Language of FatMouse Time Limit: 10 Seconds Memory Limit: 32768 KB We all know that FatMouse do ...
- ZOJ 3674 Search in the Wiki(字典树 + map + vector)
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4917 题意:每一个单词都一些tips单词. 先输入n个单词和他们的t ...
- zoj 1109 Language of FatMouse(map映照容器的典型应用)
题目连接: acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1109 题目描述: We all know that FatMouse doe ...
- zoj 1109 Language of FatMouse(map)
Language of FatMouse Time Limit: 10 Seconds Memory Limit: 32768 KB We all know that FatMouse do ...
- ZOJ 1109 Language of FatMouse
较简单字典树,每输入一对字符串,前一个放在字典(数组)中,后一个插入字典树中,并将其最终的flag赋为前一个在数组中的下标,再就好找了.输入的处理方法要注意一下. 代码: #include <i ...
- zoj1109 水题(大神绕道) Language of FatMouse
Language of FatMouse Time Limit: 10 Seconds Memory Limit:32768 KB We all know that FatMouse doe ...
- zoj1109-Language of FatMouse 【字典树】
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=109 Language of FatMouse Time Limit: 10 S ...
- ZOJ 2706 Thermal Death of the Universe (线段树)
题目链接:ZOJ 2706 Thermal Death of the Universe (线段树) 题意:n个数.m个操作. 每一个操作(a,b)表示(a,b)全部值更新为这个区间的平均数:1.当前的 ...
- poj 2503:Babelfish(字典树,经典题,字典翻译)
Babelfish Time Limit: 3000MS Memory Limit: 65536K Total Submissions: 30816 Accepted: 13283 Descr ...
随机推荐
- Oracle提示“资源正忙,需指定nowait”的解决方案
Oracle提示“资源正忙,需指定nowait”的解决方案 | T 本文我们主要介绍了Oracle数据库操作表时提示“资源正忙,需指定nowait”的解决方案,希望能够对您有所帮助. AD:51CT ...
- android LinearLayout android:layout_weight 作用,固定比例
android 中的 LinearLayout 是线性布局有水平布局horizontal 垂直布局vertical .本文针对 水平布局horizontal 布局的weight属性做一个标记,以免 ...
- Eclipse Rcp
http://blog.csdn.net/soszou/article/details/7996748
- express中路由设置的坑-----1
router.get('/commodities/sortable', utils.logged, function (req, res) { Commodity.find({force_top:tr ...
- SVN使用Tips
1. 如果在本地删除了某个文件,在Cornerstone上的本地仓库会出现D的标志,并且文件不存在. 这时,只需要将该文件提交到服务器上,本地仓库就会清除了已删除的文件的标识,同时,服务器上也会自动删 ...
- mac电脑批量解压android apk文件图形化工具--apkDecode
mac电脑apk文件解压软件,简单的用图形界面将apktools包装了下,使用起来非常简单,可以将apk文件批量解压缩,方便大家查看一些东东,仅供学习目的. 使用步骤如下: 1 下载apkDecode ...
- android 内存优化
OOM 内存泄漏引起很多问题: 1:节目卡顿.反应慢(高内存使用情况JVM 虚拟机的频繁离职GC) 2:消失 3:直接崩溃 ANDROID 内存面临的问题 1: 有限的堆内存,原始仅仅有16M 2:内 ...
- HDU 3123-GCC(递推)
GCC Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others) Total Subm ...
- RPC介绍以及编程
1 RPC介绍 RPC(Remote Procedure Call Protocol)——远程过程调用协议,它是一种通过网络从远程计算机程序上请求服务,而不需要了解底层网络技术的协 议. RPC采用客 ...
- *max_element函数和*min_element函数
C++中*max_element(v.begin,v.end)找最大元素*min_element(v.begin,v.end)找最小元素. 数组: #include<iostream> # ...