zoukankan      html  css  js  c++  java
  • uva536 二叉树重建ac

    #include <cstdio>
    #include <cstring>
    #include <cstdlib>
    #include <map>
    using namespace std;
    map<long long, int> weight2count;
    char s[1<<25];
    int sum;
    
    void build()//不一定要递归建树, [ 的数量减去 ] 数量就是结点的深度
    {
     int len = strlen(s);
     int depth = 0;
     sum = 0;
     weight2count.clear();
     for (int i = 0;i < len;i++)
     {
      switch (s[i])
      {
      case '[':
       depth++;break;
      case ']':
       depth--;break;
      case ',':
       break;
      default:
      {
       long long w = s[i] - 48;//'0'
       int k;
       for (k = i + 1;s[k] >= '0'&&s[k] <= '9';k++)
       {
        w = w * 10 + s[k] - 48;
       }
       i = k - 1;
       sum++;weight2count[w << depth]++;//!以不同节点为基准若出现树的总质量相等,则以某个节点为基准另一个结点不需改变质量
      }
      }
     }
    }
     
     
    int main(void)
    {
     int n;
     scanf("%d", &n);
     while (n--)
     {
      scanf("%s", s);
      build();
      int max = 0;
      for (auto it = weight2count.begin();it != weight2count.end();it++)
       if (max < it->second)max = it->second;
      printf("%d
    ", sum-max);
     }
     return 0;
    }
  • 相关阅读:
    C++-struct类的新特性当class用
    rbenv、fish 與 VSCode 設置之路
    angularJS进阶阶段(4)
    插入排序
    Vimium
    Design Patterns 25
    Mysql(或者sqlite), Mongo中update Column + 1
    Hexo
    继承
    Gradle的依赖方式——Lombok在Gradle中的正确配置姿势
  • 原文地址:https://www.cnblogs.com/schsb/p/7898790.html
Copyright © 2011-2022 走看看