没啥事用C语言写一个Trie tree玩玩,支持中英文,用g++编译通过
#include <cstdio>
#include <cstdlib>
#include <vector> #define ALPHABETS 2600000
#define CASE 0
#define MAX_WORD_SIZE 25 using namespace std; struct node
{
struct node *parent;
struct node *children[ALPHABETS];
vector<int> occurrences;
}; int IsGB(char *pText)
{
unsigned char sqChar[];
sqChar[] = *pText;
if (sqChar[] >= 0xa1)
if (sqChar[] == 0xa3)
return ; //全角字符
else
return ; //汉字
else
return ; //英文、数字、英文标点
} void insertWord(struct node *trieTree, char *word, int index)
{
struct node *traverse = trieTree; while (*word != '\0')
{
if (IsGB(word)!=)
{
if (traverse->children[-abs(*word)] == NULL)
{
traverse->children[-abs(*word)] = (struct node *)calloc(, sizeof(struct node));
traverse->children[-abs(*word)]->parent = traverse; // Assigning parent
}
traverse = traverse->children[-abs(*word)];
++word;
++word;
}
else
{
if (traverse->children[*word - CASE] == NULL)
{
traverse->children[*word - CASE] = (struct node *)calloc(, sizeof(struct node));
traverse->children[*word - CASE]->parent = traverse; // Assigning parent
}
traverse = traverse->children[*word - CASE];
++word;
}
} traverse->occurrences.push_back(index);
} struct node *searchWord(struct node *treeNode, char *word)
{
while (*word != '\0')
{
if (IsGB(word)!=)
{
if (treeNode->children[-abs(*word)] != NULL)
{
treeNode = treeNode->children[-abs(*word)];
++word;
++word;
}
else
{
break;
}
}
else
{
if (treeNode->children[*word - CASE] != NULL)
{
treeNode = treeNode->children[*word - CASE];
++word;
}
else
{
break;
}
}
} if (*word == '\0' && treeNode->occurrences.size() != )
{
printf("Word found");
return treeNode;
}
else
{
// Word not found
printf("Word not found");
return NULL;
}
} void removeWord(struct node *trieTree, char *word)
{
struct node *trieNode = searchWord(trieTree, word); if (trieNode == NULL)
{
return;
} trieNode->occurrences.pop_back();
bool noChild = true; int childCount = ;
int i; for (i = ; i < ALPHABETS; ++i)
{
if (trieNode->children[i] != NULL)
{
noChild = false;
++childCount;
}
} if (!noChild)
{
return;
} struct node *parentNode; while (trieNode->occurrences.size() == && trieNode->parent != NULL && childCount == )
{
childCount = ;
parentNode = trieNode->parent; for (i = ; i < ALPHABETS; ++i)
{
if (parentNode->children[i] != NULL)
{
if (trieNode == parentNode->children[i])
{
parentNode->children[i] = NULL;
free(trieNode);
trieNode = parentNode;
}
else
{
++childCount;
}
}
}
}
} void lexicographicalPrint(struct node *trieTree, vector<char> word)
{
int i;
bool noChild = true; if (trieTree->occurrences.size() != )
{
vector<char>::iterator charItr = word.begin(); while (charItr != word.end())
{
printf("%c", *charItr);
++charItr;
}
printf(" -> @ index -> "); vector<int>::iterator counter = trieTree->occurrences.begin(); while (counter != trieTree->occurrences.end())
{
printf("%d, ", *counter);
++counter;
} printf("\n");
} for (i = ; i < ALPHABETS; ++i)
{
if (trieTree->children[i] != NULL)
{
noChild = false;
word.push_back(CASE + i); lexicographicalPrint(trieTree->children[i], word);
word.pop_back();
}
} } int main()
{
int n, i;
vector<char> printUtil; struct node *trieTree = (struct node *)calloc(, sizeof(struct node));
char word[MAX_WORD_SIZE]; printf("Enter the number of words-\n");
scanf("%d", &n); for (i = ; i <= n; ++i)
{
scanf("%s", word);
insertWord(trieTree, word, i);
} lexicographicalPrint(trieTree, printUtil); // scanf("%s", word);
// removeWord(trieTree, word); // printf("\n"); //
// lexicographicalPrint(trieTree, printUtil); printf("\nEnter the Word to be search - ");
scanf("%s", word);
searchWord(trieTree, word); return ;
}
编译:g++ tr.cpp -o tr.exe
没啥事用C语言写一个Trie tree玩玩,支持中英文,用g++编译通过的更多相关文章
- 用C语言写一个“事件”的模拟程序
源:用C语言写一个“事件”的模拟程序 Example.c //定义一个函数指针 func int (*func) (void); //调用该函数相当于触发了事件. //该事件触发后,会检查函数指针fu ...
- 网络编程—【自己动手】用C语言写一个基于服务器和客户端(TCP)!
如果想要自己写一个服务器和客户端,我们需要掌握一定的网络编程技术,个人认为,网络编程中最关键的就是这个东西--socket(套接字). socket(套接字):简单来讲,socket就是用于描述IP地 ...
- 【阿菜做实践】利用go语言写一个简单的Pow样例
本篇博客的主要内容是用go写一个简单的Proof-of-Work共识机制,不涉及到网络通信环节,只是一个本地的简单demo.开发IDE用的是JB Golang. 整个项目的文件结构如下: PoWdem ...
- Linux内核学习--写一个c程序,并在内核中编译,运行
20140506 今天开始学习伟大的开源代表作:Linux内核.之前的工作流于几个简单命令的应用,因着对Android操作系统的情愫,“忍不住”跟随陈利君老师的步伐,开启OS内核之旅.学习路径之一是直 ...
- 用html语言写一个功课表
今天在网上看了一个关于html的教程,主要是讲表格,看完之后认为有必要上机试试.于是就写了以下的一段代码. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvb ...
- java语言写一个建议的五子棋
经过16天的java学习,也学得了不少关于Java方面的知识,我想分享一下我用java写的一个简单的五子棋. 游戏规则: (1)对局双方各执一色棋子.(2)空棋盘开局.(3)白先.黑后,交替下子,每次 ...
- 用c语言写一个函数把十进制转换成十六进制(转)
#include "stdio.h" int main() { int num=0;int a[100]; int i=0; int m=0;int yushu; char hex ...
- 用java语言写一个简易版本的登录页面,包含用户注册、用户登录、用户注销、修改密码等功能
package com.Summer_0421.cn; import java.util.Arrays; import java.util.Scanner; /** * @author Summer ...
- 用python语言写一个简单的计算器
假如我们有这样一个式子: 1 - 2 * ( (60-30 +(-40/5) * (9-2*5/3 + 7 /3*99/4*2998 +10 * 568/14 )) - (-4*3)/ (16-3*2 ...
随机推荐
- (9)How to take a picture of a black hole
https://www.ted.com/talks/katie_bouman_what_does_a_black_hole_look_like/transcript 00:13In the movie ...
- mysql的myBatis,主键自增设置
方法一: insert id="insert" parameterType="Person" useGeneratedKeys="true" ...
- POJ 3110 Jenny's First Exam (贪心)
题意:告诉你n 个科目的考试日期,在考试当天不能复习,每一个科目的最早复习时间不能早于考试时间的t天,每一天你可以复习完一科,也只能复习一科,求最晚的复习时间!. 析:由于题目给定的时间都在1900 ...
- HDU 5656 CA Loves GCD (容斥)
题意:给定一个数组,每次他会从中选出若干个(至少一个数),求出所有数的GCD然后放回去,为了使自己不会无聊,会把每种不同的选法都选一遍,想知道他得到的所有GCD的和是多少. 析:枚举gcd,然后求每个 ...
- FoonSunCMS-Word图片上传功能-Xproer.WordPaster
1.1. 与FoosunCMS 3.1.0930整合 基于WordPaster-asp-CKEditor4.x示例 下载地址:http://www.ncmem.com/download/WordPas ...
- LRU ,LRUW,CKPT-Q
原文出处:http://www.itpub.net/thread-1631537-1-1.html http://www.linuxidc.com/Linux/2012-07/66767.htm ...
- iOS笔记之UIKit_UIButton
//UIButton的基本属性 _btn = [UIButton buttonWithType:UIButtonTypeCustom]; _btn.frame = CGRectMake(0, 200, ...
- 集成算法(chapter 7 - Hands on machine learning with scikit learn and tensorflow)
Voting classifier 多种分类器分别训练,然后分别对输入(新数据)预测/分类,各个分类器的结果视为投票,投出最终结果: 训练: 投票: 为什么三个臭皮匠顶一个诸葛亮.通过大数定律直观地解 ...
- AEAI DP开发统计分析
1 背景概述 平时做统计分析都是调rest服务,给前台提供数据,然后在管理控制台里配置portlet.但并不是所有的项目都会用到portal,这时就需要在AEAI DP应用开发平台里开发统计分析了,下 ...
- 《AngularJs实战》学习笔记(慕课网)
1. Controller使用过程中的注意点 不要试图去复用Controller, 一个控制器一般只负责一小块视图 不要在Controller中操作DOM, 这不是控制器的职责. 封装在指令里. 不要 ...