`

hdu 4041 Eliminate Witches(模拟题)

阅读更多

Eliminate Witches!

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 511    Accepted Submission(s): 216

Problem Description
Kaname Madoka is a Magical Girl(Mahou Shoujo/Puella Magi). The duty of a Magical Girl is to eliminate Witches(Majo). Though sounds horrific, it is not a hard job for her as a powerful magical girl.

One day Madoka is eliminating Witches as usual. This time she is facing a maze full of Witches. The maze consists of rooms, each lives exactly one Witch. And there is exactly one path from one room to another. So you see, the maze can be represented as a tree, with rooms regarded as nodes on the tree.

Madoka eliminates Witches according to the following rules:
1. At first, Madoka enters the root node room of the maze.
2. If the room Madoka enters lives a Witch, Madoka will eliminate it at once, and the Witch disappear.
3. If the room has child node rooms with Witches, Madoka will choose the leftmost one and enter it.
4. Madoka won't go back to the parent node room of a room X until Witches living in child node rooms of X are all eliminated.

See the figure below for details about a sample maze. The numbers inside nodes indicate the order of elimination of the corresponding Witches, the strings below nodes are names of Witches, and the arrow shows the tracks Madoka travels:

After finishes her task, Madoka just make a brief log like this:
"walpurgis(charlotte(patricia,gertrud),elly,gisela)"
which represents the tree-like maze identifying rooms by the names of Witches living in them.

Akemi Homura, a classmate of Madoka, also a Magical Girl, is a mad fan of her. She wants to take detailed notes of everything Madoka do! Apparently the log Madoka made is hard to read, so Homura decide to make a new one of her own.

The new log should contain the following information:
1. The number of rooms of the maze
2. Names of Witches in all rooms.
3. The tracks Madoka travels. (represented by the number identifying the node)

So the new log should be like this:
6
walpurgis
charlotte
patricia
gertrud
elly
gisela
1 2
2 3
3 2
2 4
4 2
2 1
1 5
5 1
1 6
6 1

However, the maze may be very large, so Homura nees a program to help her.

 

Input
The first line contains an integer T(T<=20), indicating the number of test cases.
For each case there is only one string on a line, Madoka's log.
It is guaranteed that the maze consists of at most 50000 rooms, and the names of Witches is a string consists of at most 10 lowercase characters, while the string of Madoka's log consists of at most 1000000 characters, which are lowercase characters, '(', ')' or ','.

 

Output
For each case, you should output the detailed log.
The first line an integer N, the number of rooms of the maze.
The following N lines, each line a string, the name of the Witches, in the order of elimination.
The following 2(N-1) lines, each line two integers, the number of two nodes indicating the path Madoka passes.
Output a blank line after each case.

 

Sample Input
3 walpurgis(charlotte(patricia,gertrud),elly,gisela) wuzetian nanoha(fate(hayate))

 

Sample Output
6 walpurgis charlotte patricia gertrud elly gisela 1 2 2 3 3 2 2 4 4 2 2 1 1 5 5 1 1 6 6 1 1 wuzetian 3 nanoha fate hayate 1 2 2 3 3 2 2 1
 

          题目大意:水题!模拟题!英语题!不解释,直接代码。遇到这些题就是要小心啊!不要粗心大意!

链接:http://acm.hdu.edu.cn/showproblem.php?pid=4041

代码:

#include <iostream>
#include <stdio.h>
#include <cstring>
#include <string>
#include <stack>
using namespace std;

char str[1000005], ss[1000005];
char sh[105];
char ch[50005][105];

int main()
{
    int i, k, len, t, ans, num;
    bool tar;
    scanf("%d", &t);
    while(t--)
    {
        stack<int> ST;
        scanf("%s", str);
        len = strlen(str);
        tar = true;
        num = 0;
        ans = 0;
        for(i = 0; i < len; i++)
        {
            if(str[i]>='a' && str[i]<='z')
            {
                sh[ans++] = str[i];
                tar = false;
            }
            else
            {
                if(tar) continue;
                sh[ans] = 0;
                strcpy(ch[num], sh);
                num++;
                ans = 0;
                tar = true;
            }
        }
        if(!tar)
        {
            sh[ans] = 0;
            strcpy(ch[num], sh);
            num++;
        }
        printf("%d\n", num);
        for(i = 0; i < num; i++)
        {
            printf("%s\n", ch[i]);
        }
        k = 1;
        for (i = 0; i < len; i++)
        {
            if(str[i] == '(') continue;
            if(str[i] >= 'a' && str[i] <= 'z' && (i+1 >= len || str[i+1] < 'a' || str[i+1] > 'z'))
            {
                if(ST.size() > 0)
                    printf ("%d %d\n", ST.top(), k);
                ST.push(k++);
            }
            if(str[i] == ',' || str[i] == ')')
            {
                printf("%d ", ST.top());
                ST.pop();
                printf("%d\n", ST.top());
            }
        }
        printf ("\n");
    }

    return 0;
}

 

0
0
分享到:
评论

相关推荐

    ACM HDU题目分类

    ACM HDU 题目分类 ACM HDU 题目分类是指对 HDU 在线判题系统中...1030 简单题,可用模拟过 等等。 ACM HDU 题目分类是一个非常重要的参考资源,对于编程选手来说,掌握这些分类可以帮助他们更好地理解和解决问题。

    hdu.rar_hdu

    HDU(杭州电子科技大学在线评测系统)是一个深受程序员喜爱的在线编程练习平台,它提供了丰富的算法题目供用户挑战,帮助他们提升编程技能和算法理解能力。"hdu.rar_hdu"这个压缩包文件很可能是某位程序员整理的他在...

    HDU_2010.rar_hdu 2010_hdu 20_hdu acm20

    "hdu"可能是指杭州电子科技大学(Hangzhou Dianzi University),这所学校经常举办ACM编程竞赛,并有自己的在线判题系统——HDU Online Judge,供参赛者提交代码并测试解决方案。 【压缩包子文件的文件名称列表】中...

    HDU题目java实现

    【标题】"HDU题目java实现"所涉及的知识点主要集中在使用Java编程语言解决杭州电子科技大学(HDU)在线评测系统中的算法问题。HDU是一个知名的在线编程竞赛平台,它提供了大量的算法题目供参赛者练习和提交解决方案...

    hdu1250高精度加法

    本题(hdu1250)主要考察的就是如何通过编程实现高精度加法,并解决一个特定的数学问题。 #### 题目解析 根据题目描述,该题目编号为HDU1250,其核心在于利用高精度加法解决问题。具体地,题目涉及到了斐波那契数列...

    HDU DP动态规划

    【描述】提到的"HDU的一题"可能是指HDU(杭州电子科技大学)在线判题系统中的一道动态规划题目。这个系统经常被用来举办各类编程竞赛,包括ACM/ICPC(国际大学生程序设计竞赛)等。描述中的省略部分可能包含了具体...

    HDU1059的代码

    HDU1059的代码

    hdu.rar_HDU 1089.cpp_OJ题求和_hdu_horsekw5_杭电obj

    【标题】"hdu.rar_HDU 1089.cpp_OJ题求和_hdu_horsekw5_杭电obj" 提供的信息是关于一个压缩文件,其中包含了一个名为 "HDU 1089.cpp" 的源代码文件,这个文件是为了解决杭州电子科技大学(Hangzhou Dianzi ...

    hdu1001解题报告

    hdu1001解题报告

    hdu 1574 passed sorce

    hdu 1574 passed sorce

    ACM HDU

    【标签】"ACM题解 HDU"意味着这是一个关于如何解答HDU ACM题目的资源,可能包含了解题思路、算法解析、代码实现等方面的内容。这样的资料对于准备ACM比赛的选手或者希望提升算法能力的程序员来说非常有价值。 在...

    hdu2101解决方案

    hdu2101AC代码

    HDU acm-PPT课件

    模拟竞赛是提高实战能力的有效方式,通过参与在线判题平台(如HDU OJ)的练习,可以锻炼快速阅读题目、理解和解决问题的能力。同时,ACM竞赛强调团队协作,学习如何与队友有效沟通,分工合作,共同解决问题,也是...

    杭电ACMhdu1163

    【标题】:杭电ACMhdu1163 【描述】:这是一道源自杭州电子科技大学(Hangzhou Dianzi University,简称HDU)的ACM编程竞赛题目,编号为1163。这类问题通常需要参赛者利用计算机编程解决数学、逻辑或算法上的挑战,...

    hdu 5007 Post Robot

    hdu 5007 Post Robot 字符串枚举。 暴力一下就可以了。

    Hdu1000—2169部分代码

    标题"HDu1000—2169部分代码"可能指的是作者在HDU OJ上解决了一系列题目,从1000号题到2169号题的部分代码。这通常意味着这些代码是用于解决特定算法问题的,并且这些提交已经被平台验证为正确,成功地通过了所有...

    hdu acm1166线段树

    hdu 1166线段树代码

    HDU.rar_hdu_hdu07_com_shownv9b_www.563hdu.

    【描述】"HDU一部分题目原码,大部分是可运行的,可能有几题不完整" 表明这个压缩包中的内容主要是HDU平台上一些编程题目的解决方案,源代码文件。这些代码大部分是可以直接运行的,意味着它们已经实现了题目所要求...

    HDU最全ac代码

    HDU(Hangzhou Dianzi University)是国内外知名的在线编程竞赛平台,主要服务于ACM/ICPC(国际大学生程序设计竞赛)以及相关的算法训练。"HDU最全ac代码"这个压缩包很可能是包含了在HDU平台上解题通过的完整源代码...

    hdu动态规划算法集锦

    根据提供的信息,我们可以总结出以下关于“hdu动态规划算法集锦”的知识点: ### 动态规划基础概念 动态规划是一种解决多阶段决策问题的方法,它通过将原问题分解为互相重叠的子问题,利用子问题的解来构建原问题...

Global site tag (gtag.js) - Google Analytics