好开心,手动自己按照字典树的思想用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. IZ65534: 'JAVA.LANG.CLASSFORMATERROR' ERROR FOR A VALID IDENTIFIER

    PAR status Closed as program error. Error description Error Message: The java class could not be loa ...

  2. 2016 Multi-University Training Contest 8 总结

    回家之后一堆的事情,最后两场多校都没怎么参加,终于现在有些时间可以把第八场的总结补上. 欣君开局看出06题公式,我照着写,一A,差一分钟拿到FB,有点可惜. 磊哥觉得11题水题,写了一下,一A. 欣君 ...

  3. poj2163

    #include <stdio.h> #include <stdlib.h> int main() { ; ]; scanf("%d %d %d",& ...

  4. javaio学习笔记-字符流类(2)

    1.java.io包中的字符流类-FileReader和FileWriter: BufferedReader:缓存的输入字符流; BufferedWriter:缓存的输出字符流; FileReader ...

  5. 加入收藏夹的js代码(求兼容chrome浏览器的代码)

    从网上找了加入收藏夹的js代码,但不兼容chrome,不知道有没有兼容chrome的相关代码,希望有知道的告诉一下,谢谢! 代码如下 $("#id").click(function ...

  6. new、delete与malloc、free的详解

    内容清单: 1.  C语言中的函数malloc和free 2.  C++中的运算符new和delete 3.  new/delete与malloc/free之间的联系和区别 4.  C/C++程序的内 ...

  7. sql server varchar(10)和 nvarchar(10)存储数据长度

    ) 存储10个字母,英文标点符号等,5个汉字以及中文标点等. )存储10汉字.字母等,不区分中英文.

  8. jQuery 迭代器

    在 叶小钗 的博客中看到一段关于遍历的代码 var ajQuery = {}; function dir(elem, dir, until) { var matched = [], truncate ...

  9. JavaSE复习日记 : 递归函数

    /* * 递归函数 * 什么是递归? * 在一个方法的内部,对自身进行调用,又叫做递归调用 * * 递归和循环的编写都包括三部分: * 1. 初始值; * 2. 终止条件; * 3. 前进步长; * ...

  10. osg项目经验1<MFC+OSG中模型点选效果>

    点选主要是重载osg的GUIEventHandler, class CPickHandler : public osgGA::GUIEventHandler{ //自定义回调函数名:CPickHand ...