HDU-1251-统计难题(Trie树)(BST)(AVL)
- 字典树解法(Trie树)
Accepted 1251 156MS 45400K 949 B C++ #include"iostream"
#include"cstdlib"
#include"cstring"
#include"cstdio"
using namespace std;
struct tree {
int cnt;
tree* Next[];
} *root;
tree* init() {
tree* t = (tree*) malloc(sizeof(tree));
memset(t -> Next, NULL, sizeof(t -> Next));
t -> cnt = ;
return t;
}
void in(char* s) {
tree* now = root;
for(int i = ; s[i]; i++) {
int j = s[i] - 'a';
if(! now -> Next[j])
now -> Next[j] = init();
now = now -> Next[j];
now -> cnt++;
}
}
void out(char* s) {
tree* now = root;
for(int i = ; s[i]; i++) {
int j = s[i] - 'a';
if(!now -> Next[j]) {
puts("");
return;
}
now = now -> Next[j];
}
printf("%d\n", now -> cnt);
}
char s[];
int main() {
root = init();
while(gets(s) && s[])
in(s);
while(gets(s))
out(s);
return ;
}
- 二叉搜索树解法(BST)
Accepted 1251 358MS 18864K 1443B G++ #include "bits/stdc++.h"
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
struct BST {
char key[];
int value;
BST* lson;
BST* rson;
}*root;
char s[];
BST* init() {
BST* point = (BST*)malloc(sizeof(BST));
strcpy(point->key, s);
point->value = ;
point->lson = point->rson = NULL;
return point;
}
void insert() {
BST* father = NULL;
BST* now = root;
int cmp;
while (now != NULL) {
cmp = strcmp(now->key, s);
if (cmp == ) {
now->value++;
return;
} else if (cmp == -) {
father = now;
now = now->rson;
} else {
father = now;
now = now->lson;
}
}
if (father == NULL) {
root = init();
} else if (cmp == -) {
father->rson = init();
} else {
father->lson = init();
}
}
int query() {
BST* now = root;
while (now != NULL) {
int cmp = strcmp(now->key, s);
if (cmp == ) {
return now->value;
} else if (cmp == -) {
now = now->rson;
} else {
now = now->lson;
}
}
return ;
}
int main() {
while (gets(s) && s[]) {
int len = strlen(s);
while (len != ) {
s[len--] = '\0';
insert();
}
}
while (~scanf("%s", &s)) {
printf("%d\n", query());
}
return ;
} - 平衡二叉搜索树解法(AVL)
Accepted 1251 343MS 24648K 3885B G++ #include "bits/stdc++.h"
using namespace std;
typedef long long LL;
const int INF = 0x3f3f3f3f;
struct AVL {
char key[];
int value;
int height;
AVL* father;
AVL* lson;
AVL* rson;
}*root;
AVL* fafa;
AVL* fa;
AVL* me;
char key[];
AVL* init(AVL* fa) {
AVL* point = (AVL*)malloc(sizeof(AVL));
strcpy(point->key, key);
point->value = ;
point->father = fa;
point->height = ;
point->lson = point->rson = NULL;
return point;
}
void updateHeight(AVL* point) {
int lheight = point->lson == NULL ? : point->lson->height;
int rheight = point->rson == NULL ? : point->rson->height;
point->height = max(lheight, rheight) + ;
if (point->father != NULL) {
updateHeight(point->father);
}
}
bool unbalance(AVL* point) {
me = point;
fa = point->father;
fafa = fa->father;
if (fafa == NULL) {
return false;
}
int lheight = fafa->lson == NULL ? : fafa->lson->height;
int rheight = fafa->rson == NULL ? : fafa->rson->height;
if (abs(lheight - rheight) > ) {
return true;
}
return unbalance(fa);
}
void leftRotate(AVL* fa, AVL* me) {
AVL* fafa = fa->father;
me->father = fafa;
if (fafa != NULL) {
if (fafa->lson == fa) {
fafa->lson = me;
} else {
fafa->rson = me;
}
}
fa->rson = me->lson;
if (me->lson != NULL) {
me->lson->father = fa;
}
fa->father = me;
me->lson = fa;
updateHeight(fa);
}
void rightRotate(AVL* fa, AVL* me) {
AVL* fafa = fa->father;
me->father = fafa;
if (fafa != NULL) {
if (fafa->lson == fa) {
fafa->lson = me;
} else {
fafa->rson = me;
}
}
fa->lson = me->rson;
if (me->rson != NULL) {
me->rson->father = fa;
}
fa->father = me;
me->rson = fa;
updateHeight(fa);
}
void rebalance() {
if (fafa->lson == fa && fa->lson == me) {
rightRotate(fafa, fa);
return;
}
if (fafa->rson == fa && fa->rson == me) {
leftRotate(fafa, fa);
return;
}
if (fafa->lson == fa && fa->rson == me) {
leftRotate(fa, me);
rightRotate(fafa, me);
return;
}
rightRotate(fa, me);
leftRotate(fafa, me);
}
void insert() {
AVL* father = NULL;
AVL* now = root;
int cmp;
while (now != NULL) {
if (strcmp(now->key, key) == ) {
now->value++;
return;
}
father = now;
cmp = strcmp(now->key, key);
if (cmp == -) {
now = now->rson;
} else {
now = now->lson;
}
}
if (father == NULL) {
root = init(NULL);
return;
} else if (cmp == -) {
father->rson = init(father);
updateHeight(father);
if (unbalance(father->rson)) {
rebalance();
}
} else {
father->lson = init(father);
updateHeight(father);
if (unbalance(father->lson)) {
rebalance();
}
}
}
int query() {
AVL* now = root;
while (now != NULL) {
int cmp = strcmp(now->key, key);
if (cmp == ) {
return now->value;
} else if (cmp == -) {
now = now->rson;
} else {
now = now->lson;
}
}
return ;
}
int main() {
while (gets(key) && key[]) {
int len = strlen(key);
while (len != ) {
key[len--] = '\0';
insert();
if (root->father != NULL) {
root = root->father;
}
}
}
while (~scanf("%s", key)) {
printf("%d\n", query());
}
return ;
}
HDU-1251-统计难题(Trie树)(BST)(AVL)的更多相关文章
- HDU - 1251 统计难题(trie树)
Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀). Input输入数据的第一部 ...
- hdu 1251 统计难题(trie树入门)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- hdu 1251 统计难题 (字典树(Trie)<PS:C++提交不得爆内存>)
统计难题Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submis ...
- hdu 1251 统计难题 (字典树入门题)
/******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...
- hdu 1251 统计难题 trie入门
统计难题 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本 ...
- HDOJ/HDU 1251 统计难题(字典树啥的~Map水过)
Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己 ...
- HDU 1251 统计难题(Trie)
统计难题 [题目链接]统计难题 [题目类型]Trie &题解: Trie的模板题,只不过这题坑点在没给数据范围,改成5e5就可以过了,用的刘汝佳蓝书模板 &代码: #include & ...
- hdu 1251 统计难题 字典树第一题。
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others)Total Submi ...
- hdu 1251 统计难题(字典树)
统计难题 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131070/65535 K (Java/Others) Total Subm ...
随机推荐
- SQL基础教程(第2版)第2章 查询基础:2-1 SELECT语句基础
● 通过指定DISTINCT可以删除重复的行.● 为列设定显示用的别名. ■列的查询 通过 SELECT 语句查询并选取出必要数据的过程称为查询(query). 该 SELECT 语句包含了 SELE ...
- one_day_one_linuxCmd---sz命令
<坚持每天学习一个 linux 命令,今天我们来学习 sz && rz 命令> 前言:我们一般通过 ssh 客户端来进行远程登录和管理的,windows主机使用 ssh 登 ...
- redis(一)----配置及安装
1. redis下载 根据自己操作系统平台下载适合的文件包: https://github.com/MSOpenTech/redis 2. redis安装 (1)解压, ...
- docker安装宝塔面板
1.下载centos docker docker pull centos:7.2.1511 2.运行镜像设置端口 docker run -d -it -p 4001:8888 -p 4000:80 - ...
- Java中常用的API(三)——缓冲区字符串
前两节中分别介绍了Object和String,这一节主要介绍StringBuffer和StringBuilder. StringBuffer 由于String是不可变的,所以导致String对象泛滥, ...
- Python笔记_第一篇_面向过程_第一部分_3.进制、位运算、编码
通过对内存这一个部分的讲解,对编程会有一个相对深入的认识.数据结构是整个内存的一个重要内容,那么关于数据结构这方面的问题还需要对进制.位运算.编码这三个方面再进行阐述一下.前面说将的数据结构是从逻辑上 ...
- Python基础学习二
Python基础学习二 1.编码 utf-8编码:自动将英文保存为1个字符,中文3个字符.ASCll编码被囊括在内. unicode:将所有字符保存为2给字符,容纳了世界上所有的编码. 2.字符串内置 ...
- Spring Cloud Alibaba 教程 | Nacos(六)
集群模式部署 前面我们已经学习了Nacos作为注册中心.配置中心的相关功能,但是我们之前启动Nacos是通过单实例模式启动的,只适合在学习和开发阶段,生产环境需要保证Nacos的高可用,所以今天我们来 ...
- 《Docekr入门学习篇》——Docker简介
Docker简介 什么是docker Docker是Docker.inc公司开源的一个基于LXC技术之上构建的Container容器引擎,源代码托管在GitHub上,基于Go语言并遵从Apache2. ...
- PAT Advanced 1041 Be Unique (20) [Hash散列]
题目 Being unique is so important to people on Mars that even their lottery is designed in a unique wa ...