• NBUT 1114 Alice's Puppets(排序统计,水)


    题意:给一棵人名树,按层输出,同层则按名字的字典序输出。

    思路:首先对每个人名做索引,确定其在哪一层,按层装进一个set,再按层输出就自动排好序了。

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 vector< set<string> > ord;    //每层一个set,自动按照字典序排好了
     4 map<string,int>   mapp;    //作哈希用
     5 
     6 int main()
     7 {
     8     //freopen("input.txt", "r", stdin);
     9     int n;
    10     string son,far;
    11     set<string> tmp;
    12     while(cin>>n)
    13     {
    14         ord.clear();
    15         for(int i=0; i<=n; i++)    ord.push_back(tmp);  //初始化
    16         mapp.clear();
    17         mapp["Alice"]=0;
    18         int k=0;
    19         for(int i=0; i<n; i++)
    20         {
    21             cin>>son>>far;
    22             k=mapp[far];
    23             mapp[son]=++k;//son的层数是far结点的层数+1
    24             ord[k].insert(son);
    25         }
    26 
    27         for(int i=1; i<=n; i++)
    28         {
    29             if(ord[i].empty()==true)    break;
    30             else
    31             {
    32                 for(set<string>::iterator it=ord[i].begin(); it!=ord[i].end(); it++)
    33                     cout<<*it<<" "<<i<<endl;
    34             }
    35         }
    36     }
    37 
    38     return 0;
    39 }
    AC代码
  • 相关阅读:
    Oracle修改字段类型
    JS解析JSON字符串
    C#Json转DataTable
    解决前台和后台传值出现中文乱码
    Oracle 存储过程简单语法
    EasyUI 冻结列
    EasyUI 数据网格行过滤
    windows计划任务
    C#日志文件
    bat 读取 ini 配置文件
  • 原文地址:https://www.cnblogs.com/xcw0754/p/4538716.html
Copyright © 2020-2023  润新知