好开心,手动自己按照字典树的思想用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. html中上标、下标、删除字、小号字等

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  2. android process bar 几种style

    process bar 圆形style记录 1. style="?android:attr/progressBarStyleSmall" //根据主题变颜色 2. holo 主题下 ...

  3. Delphi在StatusBar上绘制ProgressBar

    首先,在TForm的私有域,也就是private下设置两个变量ProgressBar.ProgressBarRect,其中ProgressBar为 TProgressBar类型,ProgressBar ...

  4. 计算机世界的道(C/ASM)生一(OS),一生二(API),二生万象(MFC/COM)——学包装技术的程序员将来会损失比较大,因为不了解本质,一旦包装过时就会被淘汰

    道生一,一生二,二生万象.OO的思想就是抽象,万象归宗,化繁为简.99%的程序员使用OO,或者所谓的类库的目的就是好用,不必了解内部实现就可以直接达到所期望的结果.这时一种生产力的进步,一种流水线式半 ...

  5. 鼠标进入与离开的消息(覆盖CM_MOUSEENTER与CM_MOUSELEAVE消息)——Windows本身没有这样的消息

    unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, ...

  6. iOS 4.2 SDK安装

    iOS 4.2 SDK安装 ◆系统版本:10.6.5 ◆硬件配置: ◆10G空间,3.5G内存,显卡:GMA950 Xcode 3.2.5和iOS 4.2 SDK下载:http://developer ...

  7. nginx File not found 错误分析与解决方法

    使用php-fpm解析PHP,出错提示如下:"No input file specified","File not found",原因是php-fpm进程找不到 ...

  8. 关于K-Means算法

    在数据挖掘中,K-Means算法是一种cluster analysis的算法,其主要是来计算数据聚集的算法,主要通过不断地取离种子点最近均值的算法. 问题 K-Means算法主要解决的问题如下图所示. ...

  9. NOPI导出Excel

    NOPI导出Excel /// <summary> /// 导出的方法 Excel样式 /// </summary> /// <param name="ds&q ...

  10. WinForm中快捷键与组合按键的设置方法

    每个窗体都有这样3个事件:KeyDown.KeyPress.KeyUp,KeyDown和KeyPress都是按键按下事件,但KeyDown用的是KeyCode跟键盘各个按键相对应,它对应Keys枚举, ...