NBUT 1114 (13.07.17)

来源:互联网 发布:ape播放软件 编辑:程序博客网 时间:2024/05/16 18:08
  • [1114] Alice's Puppets

  • 时间限制: 1000 ms 内存限制: 65535 K
  • 问题描述
  • Alice Margatroid (アリス·マーガトロイド) can control puppets. What's more, she can control her pupet to control other pupets.

    The puppets she controls directlycalled thefirst-level puppet. And the puppets which are controlled by thefirst-level puppets are called thesecond-level puppet. So as thethird,fourth and theith-level.
    Now your task is to tell me each puppet is what level.

  • 输入
  • This problem contains several cases.
    The first line of each case is an integer N (0 < N ≤ 1000), indicates the number of puppets.
    Then N lines follows. Each line has two strings, the name of the ith puppet and the controller's name. You can imagine if the controller is Alice, then the puppet is the first-level puppet.
    Every controller's name will be all appeared as a puppet's name before except Alice. Each name is no longer then 20.
  • 输出
  • For each case, you should output every puppet's names and levels, and order by level. If two puppets have the same level, then order by lexicographic.
  • 样例输入
  • 3Blahblah AlicePeps BlahblahGrugru Alice
  • 样例输出
  • Blahblah 1Grugru 1Peps 2

题意:

爱丽丝能控制木偶, 而木偶也能控制木偶

现在默认爱丽的层次是最顶端的, level为0

假如A被Alice控制, 那么A的层次 即 level = 1;

再设有一个B同时被A控制, 那么B的level= 2;

输入是一行两个名字, 第一个名字被第二个名字所控制

最后输出规则是:

按level从小到大的顺序输出

若level相同, 按名字的字典序~


AC代码如下:

#include<stdio.h>#include<string.h>#include<stdlib.h>#include<algorithm>using namespace std;#define MAXN 20+10#define N 1000+100struct pupets{char str[MAXN];int level;};pupets pupet[N];int n;int findlevel(char *p) {for(int i = 0; i < n; i++) {if(strcmp(pupet[i].str, p) == 0)return pupet[i].level;}}int cmp(const pupets &a, const pupets &b) { int mark;if(a.level != b.level)return a.level < b.level;else {mark = strcmp(a.str, b.str);if(mark < 0)return 1;elsereturn 0;}}int main() {char controled[MAXN];char controler[MAXN]; //initwhile(scanf("%d", &n) == 1) {getchar();memset(pupet, 0, sizeof(pupet));strcpy(pupet[0].str, "Alice");pupet[0].level = 0;for(int i = 1; i <= n; i++) {scanf("%s %s", controled, controler);pupet[i].level = findlevel(controler) + 1;strcpy(pupet[i].str, controled);}sort(pupet + 1, pupet + n + 1, cmp);for(int i = 1; i <= n; i++)printf("%s %d\n", pupet[i].str, pupet[i].level);}return 0;}

原创粉丝点击