好开心,手动自己按照字典树的思想用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(字典树)的更多相关文章

  1. zoj 1109 Language of FatMouse(字典树)

    Language of FatMouse Time Limit: 10 Seconds      Memory Limit: 32768 KB We all know that FatMouse do ...

  2. ZOJ 3674 Search in the Wiki(字典树 + map + vector)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4917 题意:每一个单词都一些tips单词. 先输入n个单词和他们的t ...

  3. zoj 1109 Language of FatMouse(map映照容器的典型应用)

    题目连接: acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1109 题目描述: We all know that FatMouse doe ...

  4. zoj 1109 Language of FatMouse(map)

    Language of FatMouse Time Limit: 10 Seconds      Memory Limit: 32768 KB We all know that FatMouse do ...

  5. ZOJ 1109 Language of FatMouse

    较简单字典树,每输入一对字符串,前一个放在字典(数组)中,后一个插入字典树中,并将其最终的flag赋为前一个在数组中的下标,再就好找了.输入的处理方法要注意一下. 代码: #include <i ...

  6. zoj1109 水题(大神绕道) Language of FatMouse

    Language of FatMouse Time Limit: 10 Seconds      Memory Limit:32768 KB We all know that FatMouse doe ...

  7. zoj1109-Language of FatMouse 【字典树】

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=109 Language of FatMouse Time Limit: 10 S ...

  8. ZOJ 2706 Thermal Death of the Universe (线段树)

    题目链接:ZOJ 2706 Thermal Death of the Universe (线段树) 题意:n个数.m个操作. 每一个操作(a,b)表示(a,b)全部值更新为这个区间的平均数:1.当前的 ...

  9. poj 2503:Babelfish(字典树,经典题,字典翻译)

    Babelfish Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 30816   Accepted: 13283 Descr ...

随机推荐

  1. 一个基于MINA框架应用的最简单例子

    直接上代码.关于原理和主要的API以后在说.先能跑通了在说. 主要的包:mina-core-2.0.0.jar[到官网上下载完整项目包里面还有文档和依赖包],jcl-over-slf4j-1.5.11 ...

  2. String类的实现,内部采用字符数组实现

    #include <iostream> using namespace std; class String{ public: String(const char *str = NULL); ...

  3. dg error

    startup force;ORACLE instance started. Total System Global Area 1068937216 bytesFixed Size           ...

  4. 限制div高度当内容多了溢出时显示滚动条

    <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type"content= ...

  5. 教你爱上Blocks(闭包)

    传值 Blocks是C语言的扩充功能:带有自动变量(局部变量)的匿名函数.通过Blocks,源代码中就能使用匿名函数,即不带名称的函数.在我们 的工作中,命名占据了很大一部分,函数名,变量名,属性名, ...

  6. IHttpModule与IHttpHandler的区别整理

    IHttpModule与IHttpHandler的区别整理1.先后次序.先IHttpModule,后IHttpHandler. 注:Module要看你响应了哪个事件,一些事件是在Handler之前运行 ...

  7. java获取当前路径的几种方法

    1.利用System.getProperty()函数获取当前路径: System.out.println(System.getProperty("user.dir"));//use ...

  8. android入门——UI(3)

    Spinner控件   ListView控件 一.Spinner控件 点击Spinner会弹出一个包含所有可选值的dropdown菜单,从该菜单中可以为Spinner选择一个新值. 有两种指定数据源的 ...

  9. android入门——Activity(2)

    主要内容:一.IntentFlag  二.简单复杂数据传递  三.数据回传  四.打开系统界面  五.IntentFilter匹配 一.IntentFlag 复制一段内容    来源 http://i ...

  10. jQuery获取Select选择的Text(非表单元素)和 Value(表单元素)(转)

    jQuery获取Select选择的Text和Value: 语法解释: . $("#select_id").change(function(){//code...}); //为Sel ...