HDU 4041 Eliminate witches! (Simulation of Stacks)

Source: Internet
Author: User


Eliminate witches! Time limit:2000/1000 MS (java/others) Memory limit:32768/32768 K (java/others) Total Submission (s): 1234 Accepted Submission (s): 461

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 a hard job for her as a powerful magical girl.

One day Madoka is eliminating witches as usual. This time she's facing a maze full of witches. The maze consists of rooms, each lives exactly one Witch. And there is exactly one path from one, to another. So you see, the maze can is 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 of the maze.
2. If The Madoka enters lives a Witch, Madoka would eliminate it at once, and the Witch disappear.
3. If The rooms has a child node of witches, Madoka would choose the leftmost one and enter it.
4. Madoka won ' t go back to the parent node, Class A, until witches living in child node rooms of X is all Eliminat Ed.

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 she 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 hers. She wants to take detailed notes of everything Madoka do! Apparently the log Madoka made is hard-read, so Homura decide-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 is 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 is very large, so Homura Nees a program to help her.
 
InputThe 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 for most 50000 rooms, and the names of witches are a string consists of at most 1 0 lowercase characters, while the string of Madoka ' s log consists of in most 1000000 characters, which is lowercase chara Cters, ' (', ') ' or ', '.
 
OutputFor 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 of the integers, the number of the nodes indicating the path Madoka passes.
Output a blank line after each case.
 
Sample Input
 
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
 
SourceThe 36th ACM/ICPC Asia regional Beijing site--online Contest

Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=4041

The main idea: enter a tree with words, parentheses and commas, a comma tied to a brother, a (...), a child with a in parentheses, to traverse the path of the tree in the order of the words entered

Topic Analysis: See sample can find the path is the tree node into the stack of the process, you can use the stack to simulate
' (': Indicates a child node encountered, sub-node into the stack, output father--son
', ': said the Encounter brothers, first back to the father, the current son out of the stack, output son, father, brother into the stack, output father-brother
') ': The brother who has visited the end of a layer, the last brother out of the stack, output the last brother-to-father
Note that the problem to be used in C + +, with g++ T 10 times after card a once, C + + 124ms, this problem can have an optimization, traversing the string can be preprocessed at the time of input, only take out ' (') ', ' these valid characters

#include <cstdio> #include <cstring> #include <stack>using namespace std;int Const MAX = 1e6 + 5;char S[ma    X], S2[max];char node[50005][11];int main () {int T;    scanf ("%d", &t);        while (t--) {stack <int> st;        memset (node, 0, sizeof (node));        scanf ("%s", s);        int cnt = 1, len = 0, cnt2 = 0; for (int i = 0; s[i]! = ' + '; i++) {if (S[i] >= ' A ' && s[i] <= ' z ') node[cnt            ][len++] = s[i];                    if ((s[i] = = ' (' | | s[i] = = ') ' | | s[i] = = ', ') && (s[i-1] >= ' A ' && s[i-1] <= ' z ')) {                len = 0;            cnt++;        } if (s[i] = = ' (' | | s[i] = = ') ' | | s[i] = = ', ') s2[cnt2++] = s[i];        } if (cnt = = 1) cnt++;        printf ("%d\n", cnt-1);        for (int i = 1; i < cnt; i++) printf ("%s\n", Node[i]);        CNT = 1;        St.push (1); for (int i = 0; i <Cnt2;                i++) {if (s2[i] = = ' (') {int tmp = St.top ();                St.push (++CNT);            printf ("%d%d\n", TMP, St.top ());                } else if (s2[i] = = ', ') {int tmp = St.top ();                St.pop ();                printf ("%d%d\n", TMP, St.top ());                TMP = St.top ();                 St.push (++CNT);            printf ("%d%d\n", TMP, St.top ());                } else if (s2[i] = = ') ') {int tmp = St.top ();                St.pop ();            printf ("%d%d\n", TMP, St.top ());    }} printf ("\ n"); }   }


HDU 4041 Eliminate witches! (Simulation of Stacks)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.