hdu 4041 Eliminate Witches!
Eliminate Witches!
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 417 Accepted Submission(s): 186
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:
![](http://acm.hdu.edu.cn/data/images/4041.jpg)
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.
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:
![](http://acm.hdu.edu.cn/data/images/4041.jpg)
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 ','.
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.
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))
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
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
code:
1 #include<iostream>
2 #include<stack>
3 #include<cstring>
4 #include<cstdio>
5 #include<vector>
6 #include<queue>
7 #include<string>
8
9 using namespace std;
10 int main()
11 {
12 //freopen("a.txt","r",stdin);
13 int t,i,l,d;
14 queue<string> h;
15 stack<int> g;
16 queue<int> q;
17 string a,s;
18 scanf("%d",&t);
19 while(t--)
20 {
21 while(h.size())h.pop();
22 while(g.size())g.pop();
23 while(q.size())q.pop();
24 cin>>s;
25 a="";
26 l=0;
27 d=0;
28 for(i=0;i<s.size();i++)
29 {
30 if(s[i]=='('||s[i]==')'||s[i]==','||i==s.size()-1)
31 {
32 if(s[i]>='a'&&s[i]<='z')a+=s[i];
33 if(a.size())
34 {
35 d++;
36 h.push(a);
37 }
38 if(s[i]=='(')
39 {
40 q.push(d);
41 g.push(d);3
42 walpurgis(charlotte(patricia,gertrud),elly,gisela) wuzetian nanoha(fate(hayate))
43 }
44 if(s[i]==')')
45 {
46 if(s[i-1]!=')')q.push(d);
47 q.push(g.top());
48 g.pop();
49 }
50 if(s[i]==',')
51 {
52 if(s[i-1]!=')')q.push(d);
53 q.push(g.top());
54 }
55 a="";
56 }
57 else a+=s[i];
58 }
59 printf("%d\n",d);
60 while(h.size())
61 {
62 cout<<h.front()<<endl;
63 h.pop();
64 }
65 if(d==1)
66 {
67 printf("\n");
68 continue;
69 }
70 while(q.size())
71 {
72 if(q.front()==1&&q.size()==1)break;
73 printf("%d ",q.front());
74 q.pop();
75 printf("%d\n",q.front());
76 }
77 printf("\n");
78 }
79 return 0;
80 }