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 ...
随机推荐
- C#用户控件的使用
1.添加一个用户控件 2.编辑用户控件,相当于自己定义了一个控件,和其他控件一样在窗体中使用,是一个类. 右击项目,生成一下,就可以看到窗体的工具箱上面多了一组工具,可以看到我们定义的控件login ...
- 寒假day09
今天看了论文的结构,定下了毕设论文的框架,刷了剑指offer的部分算法题.
- ElasticSearch的9200和9300端口的区别
9200用于外部通讯,基于http协议,程序与es的通信使用9200端口. 9300jar之间就是通过tcp协议通信,遵循tcp协议,es集群中的节点之间也通过9300端口进行通信.
- winform程序常用图标网站及资源
1.easyicon网站,免费下载 https://www.easyicon.net/ 2.findicons https://findicons.com/ 3.iconarchive http:// ...
- 干货|Kubernetes集群部署
Nginx-ingress Controller
Kubernetes提供了两种内建的云端负载均衡机制用于发布公共应用,一种是工作于传输层的Service资源,它实现的是TCP负载均衡器:另一种是Ingress资源,它实现的是HTTP(S)负载均衡器 ...
- LGOJ3879 TJOI2010 阅读理解
不可否认,\(TJOI\)的这道题确实不难 为本题写博客的唯一原因就是 \(STL\)大法好!!!! Description link 不简述题意了,因为实在是简单 Solution 直接\(map& ...
- 自动按键的Sendkeys工具的下载和使用
大家好! 下面介绍一款自动按键的小工具:Sendkeys 下载地址 Sendkeys.rar 按键脚本的书写规则如下: 启动本工具后,在工具中打开一个脚本文件,然后在工具中按下Ctrl+A全选所有脚本 ...
- SQL注入常用函数(注入小白的学习笔记)
在盲注的情况下,往往需要一个一个字符的去猜解,即过程中需要截取字符串 在这里整理了一下一些常用函数 由于现阶段学习不够深入,整理分类不清楚具体,不过博主会慢慢进行完善 user() 查询当前数据库用户 ...
- 使用conda管理python环境
一.动机 最近打算折腾vn.py,但只有py27版本的,因为一向习惯使用最新稳定版的,所以不得不装py27的环境,不得不说 Python的全局锁真的很烦. 身为懒癌患者,必然使用全功能的anacond ...
- 八皇后问题 2n皇后问题
Description 会下国际象棋的人都很清楚:皇后可以在横.竖.斜线上不限步数地吃掉其他棋子.如何将8个皇后放在棋盘上(有8 * 8个方格),使它们谁也不能被吃掉!这就是著名的八皇后问题. 对于某 ...