• 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[26];
      } *root;
      tree* init() {
          tree* t = (tree*) malloc(sizeof(tree));
          memset(t -> Next, NULL, sizeof(t -> Next));
          t -> cnt = 0;
          return t;
      }
      void in(char* s) {
          tree* now = root;
          for(int i = 0; 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 = 0; s[i]; i++) {
              int j = s[i] - 'a';
              if(!now -> Next[j]) {
                  puts("0");
                  return;
              }
              now = now -> Next[j];
          }
          printf("%d
      ", now -> cnt);
      }
      char s[12];
      int main() {
          root = init();
          while(gets(s) && s[0])
              in(s);
          while(gets(s))
              out(s);
          return 0;
      }
    • 二叉搜索树解法(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[11];
          int value;
          BST* lson;
          BST* rson;
      }*root;
      char s[11];
      BST* init() {
          BST* point = (BST*)malloc(sizeof(BST));
          strcpy(point->key, s);
          point->value = 1;
          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 == 0) {
                  now->value++;
                  return;
              } else if (cmp == -1) {
                  father = now;
                  now = now->rson;
              } else {
                  father = now;
                  now = now->lson;
              }
          }
          if (father == NULL) {
              root = init();
          } else if (cmp == -1) {
              father->rson = init();
          } else {
              father->lson = init();
          }
      }
      int query() {
          BST* now = root;
          while (now != NULL) {
              int cmp = strcmp(now->key, s);
              if (cmp == 0) {
                  return now->value;
              } else if (cmp == -1) {
                  now = now->rson;
              } else {
                  now = now->lson;
              }
          }
          return 0;
      }
      int main() {
          while (gets(s) && s[0]) {
              int len = strlen(s);
              while (len != 0) {
                  s[len--] = '';
                  insert();
              }
          }
          while (~scanf("%s", &s)) {
              printf("%d
      ", query());
          }
          return 0;
      }
    • 平衡二叉搜索树解法(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[11];
          int value;
          int height;
          AVL* father;
          AVL* lson;
          AVL* rson;
      }*root;
      AVL* fafa;
      AVL* fa;
      AVL* me;
      char key[11];
      AVL* init(AVL* fa) {
          AVL* point = (AVL*)malloc(sizeof(AVL));
          strcpy(point->key, key);
          point->value = 1;
          point->father = fa;
          point->height = 1;
          point->lson = point->rson = NULL;
          return point;
      }
      void updateHeight(AVL* point) {
          int lheight = point->lson == NULL ? 0 : point->lson->height;
          int rheight = point->rson == NULL ? 0 : point->rson->height;
          point->height = max(lheight, rheight) + 1;
          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 ? 0 : fafa->lson->height;
          int rheight = fafa->rson == NULL ? 0 : fafa->rson->height;
          if (abs(lheight    - rheight) > 1) {
              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) == 0) {
                  now->value++;
                  return;
              }
              father = now;
              cmp = strcmp(now->key, key);
              if (cmp == -1) {
                  now = now->rson;
              } else {
                  now = now->lson;
              }
          }
          if (father == NULL) {
              root = init(NULL);
              return;
          } else if (cmp == -1) {
              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 == 0) {
                  return now->value;
              } else if (cmp == -1) {
                  now = now->rson;
              } else {
                  now = now->lson;
              }
          }
          return 0;
      }
      int main() {
          while (gets(key) && key[0]) {
              int len = strlen(key);
              while (len != 0) {
                  key[len--] = '';
                  insert();
                  if (root->father != NULL) {
                      root = root->father;
                  }
              }
          }
          while (~scanf("%s", key)) {
              printf("%d
      ", query());
          }
          return 0;
      }
  • 相关阅读:
    数独高阶技巧入门之六——ALS
    数独高阶技巧入门之七——AIC & Nice Loop
    数独-链的理解顺序
    数独高阶技巧入门之三——Fish
    数独·唯一性技巧(Uniqueness)-2
    游戏剧本从入门到放弃
    Electron和NW.js入门笔记
    Spring boot Access-Control-Allow-Origin 问题解决
    Materialize -- 基于Material Design的主流前端响应式框架
    Ubuntu 安装 nvm
  • 原文地址:https://www.cnblogs.com/Angel-Demon/p/9677326.html
Copyright © 2020-2023  润新知