Algorithms(字典树)
来源:互联网 发布:ios6很多软件用不了 编辑:程序博客网 时间:2024/04/28 12:58
字典树
#ifndef TIRE_H_INCLUDED#define TIRE_H_INCLUDED/*** 字典树*/#define MAX 26typedef struct Node{ int num; struct Node* next[MAX];}Tire;/*** 创建一个节点*/Tire* create(void);/*** 插入一个字符串*/Tire* insert(char str[], Tire* head);/*** 搜索字符串*/int search(char str[], Tire* head);#endif // TIRE_H_INCLUDED
/*---------------------------------------------------------------------------------- * Project: Tire.cpp * Name: zwp * Date: 2014/5 *---------------------------------------------------------------------------------*/#include <stdio.h>#include <stdlib.h>#include <string.h>#include "Tire.h"#include <malloc.h>/*** 创建一个节点*/Tire* create(void){ int i; Tire* node = (Tire*)malloc(sizeof(Tire)); if(node == NULL) printf("Out of space...\n"); /* 初始化 */ for(i = 0; i < MAX; ++ i) node->next[i] = NULL; node->num = 0; return node;}/*** 插入一个字符串*/Tire* insert(char str[], Tire* head){ int i, len = strlen(str); Tire *node, *ptr = head; for(i = 1; i < len; ++ i) { int count = str[i] - 'a'; /* 若不存在该字符串 */ if(ptr->next[count] == NULL) { node = create(); ptr->next[count] = node; ptr->num++; ptr = ptr->next[count]; } /* 若存在该字符串 */ else { ptr = ptr->next[count]; } }}/*** 搜索字符串,返回该字符串出现的次数*/int search(char str[], Tire* head){ Tire* ptr = head; int len = strlen(str); int i, count = 0; for(i = 0; i < len; ++ i) { int cou = str[i] - 'a'; if(ptr->next[cou] == NULL) { printf("不存在该字符串\n"); count = 0; return 0; } else { ptr = ptr->next[cou]; count = ptr->num; } } return count;}
/*------------------------------------------------------------------------------ * Project: Main.cpp * Name: zwp * Date: 20145/ *------------------------------------------------------------------------------*/ #include "Tire.h" #include <stdio.h> #include <stdlib.h> #include <string.h> int main(int argc, char* argv[]) { Tire* node = create(); char str[10]; while(scanf("%s\n", str) ) { if(strcmp(str, "quit") == 0) break; insert(str, node); } int count = search("a", node); printf("%d\n", count); system("pause"); return 0; }
0 1
- Algorithms(字典树)
- Algorithms: Treap树
- Algorithms: Treap树
- Algorithms(线段树)
- Algorithms
- algorithms
- algorithms
- Algorithms
- ALGORITHMS
- Algorithms
- Algorithms
- Algorithms: 二叉平衡树(AVL)
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树
- 字典树。。
- 解决宿主机不能访问虚拟机CentOS中的站点
- 四川外国语大学IFC国际本科预科招生简章
- UVa 401 - Palindromes
- 从零单排PAT1007. 素数对猜想
- IIS网站服务器性能优化攻略
- Algorithms(字典树)
- id 声明的对象有什么特性?
- mybatis学习-入门(6)-实际例子,输入用户注册,并在离开用户名输入框时检查用户是否存在
- linux下非root用户如何修改root权限的文件
- 今天开通博客了,写一写我在软件开发和学习中的经历。
- g++与gcc及cc区别
- Nginx做前端,Apache做后端的配置实例
- cocoapod 慢
- sql笔试题(求课程难度)