• 字典树解法(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)的更多相关文章

  1. HDU - 1251 统计难题(trie树)

    Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己的前缀).  Input输入数据的第一部 ...

  2. hdu 1251 统计难题(trie树入门)

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

  3. hdu 1251 统计难题 (字典树(Trie)<PS:C++提交不得爆内存>)

    统计难题Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submis ...

  4. hdu 1251 统计难题 (字典树入门题)

    /******************************************************* 题目: 统计难题 (hdu 1251) 链接: http://acm.hdu.edu. ...

  5. hdu 1251 统计难题 trie入门

    统计难题 Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本 ...

  6. HDOJ/HDU 1251 统计难题(字典树啥的~Map水过)

    Problem Description Ignatius最近遇到一个难题,老师交给他很多单词(只有小写字母组成,不会有重复的单词出现),现在老师要他统计出以某个字符串为前缀的单词数量(单词本身也是自己 ...

  7. HDU 1251 统计难题(Trie)

    统计难题 [题目链接]统计难题 [题目类型]Trie &题解: Trie的模板题,只不过这题坑点在没给数据范围,改成5e5就可以过了,用的刘汝佳蓝书模板 &代码: #include & ...

  8. hdu 1251 统计难题 字典树第一题。

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others)Total Submi ...

  9. hdu 1251 统计难题(字典树)

    统计难题 Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 131070/65535 K (Java/Others) Total Subm ...

随机推荐

  1. 吴裕雄--天生自然 JAVASCRIPT开发学习:函数调用

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

  2. (函数)P1149 火柴棒等式

    题解: #include<stdio.h>int a[10]={6,2,5,5,4,5,6,3,7,6};int num(int n){                          ...

  3. TCP连接为什么三次握手四次挥手

    前几天面试某电商被问住了,问的很细,我就说了说连接过程,必然凉凉.在csdn上找了一篇很详细的博客.https://blog.csdn.net/hyg0811/article/details/1023 ...

  4. Django的URL路由基础

    一.概述 URL路由在Django项目中的体现就是urls.py文件,这个文件可以有很多个,但绝对不会在同一目录下.实际上Django提倡项目有个根urls.py,各app下分别有自己的一个urls. ...

  5. 计蒜客 置换的玩笑(DFS)

    传送门 题目大意: 小蒜头又调皮了.这一次,姐姐的实验报告惨遭毒手. 姐姐的实验报告上原本记录着从 1 到 n 的序列,任意两个数字间用空格间隔.但是“坑姐”的蒜头居然把数字间的空格都给删掉了,整个数 ...

  6. Linux中的错误重定向你真的懂吗

    在很多定时任务里.shell里我们往往能看到 "2>&1",却不知道这背后的原理. 举个例子: * 1 * * * test.sh > /dev/null 2& ...

  7. 面向对象 part6 继承

    继承 js实现的是实现继承/也就是继承实际的方法 //主要依赖:原型链 //基本思路: 就是一个引用类型继承另一个引用类型的属性和方法 详细:构造函数,实例,原型之间的关系.每个构造函数都有一个原型对 ...

  8. 【转】 java类的加载和执行顺序

    1.先执行Test类的静态代码块后执行Test类的main方法,说明要执行类的方法需要先加载这个类. 2.在创建ClassB的对象时,先去加载了父类ClassA.说明加载子类时如果没有加载父类,会先加 ...

  9. git的基础使用

    GIT """ 什么是git:版本控制器 - 控制的对象是开发的项目代码 代码开发时间轴:需求1 > 版本库1 > 需求2 > 版本库2 > 版本 ...

  10. setTimeout()执行时序

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xht ...