首页 > 其他分享 >HDU 4041 Eliminate Witches!

HDU 4041 Eliminate Witches!

时间:2022-11-09 19:07:09浏览次数:44  
标签:HDU Madoka 4041 tot Tot rooms Witches maze


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: 




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

简单的栈应用

#include<stdio.h>
#include<string.h>
#include<stack>
using namespace std;
int T, tot, Tot, b[1000005][2];
char s[1000005];
char ss[50005][11];

int main()
{
scanf("%d", &T);
while (T--)
{
stack <int> a;
scanf("%s", s);
for (int i = tot = Tot = 0; s[i]; i++)
{
if (s[i] >= 'a'&&s[i] <= 'z')
{
for (int j = 0; s[i] >= 'a'&&s[i] <= 'z'; i++, ss[tot][++j] = 0)
{
ss[tot][j] = s[i];
}
++tot; a.push(tot);
}
switch (s[i])
{
case '(':{ b[Tot][0] = a.top(); b[Tot++][1] = tot + 1; break; }
case ',':{
int t = a.top(); a.pop();
b[Tot][0] = t; b[Tot++][1] = a.top();
b[Tot][0] = a.top(); b[Tot++][1] = tot + 1;
break;
}
case ')':{
int t = a.top(); a.pop();
b[Tot][0] = t; b[Tot++][1] = a.top();
break;
}
default:i--;
}
}
printf("%d\n", tot);
for (int i = 0; i < tot; i++) printf("%s\n", ss[i]);
for (int i = 0; i < Tot; i++) printf("%d %d\n", b[i][0], b[i][1]);
printf("\n");
}
return 0;
}



标签:HDU,Madoka,4041,tot,Tot,rooms,Witches,maze
From: https://blog.51cto.com/u_15870896/5838281

相关文章

  • HDU 1237 简单计算器
    ProblemDescription读入一个只包含+,-,*,/的非负整数计算表达式,计算该表达式的值。Input测试输入包含若干测试用例,每个测试用例占一行,每行不超过200个字......
  • HDU 4739 Zhuge Liang's Mines
    ProblemDescriptionIntheancientthreekingdomperiod,ZhugeLiangwasthemostfamousandsmartestmilitaryleader.HisenemywasShimaYi,whoalways......
  • HDU 3608 最长回文
    ProblemDescription给出一个只由小写英文字符a,b,c...y,z组成的字符串S,求S中最长回文串的长度.回文就是正反读都是一样的字符串,如aba,abba等 Inp......
  • HDU 5591 ZYB's Game
    ProblemDescription withhisclassmatesinhiking:ahostkeepsanumberin  loses,orthehostwilltellalltheplayersthatthenumbernowisbigger......
  • HDU 4433 locker
    ProblemDescriptionApasswordlockerwithNdigits,eachdigitcanberotatedto0-9circularly.Youcanrotate1-3consecutivedigitsupordownino......
  • HDU 5586 Sum
    ProblemDescriptionThereisanumbersequence ,youcanselectainterval[l,r]ornot,allthenumbers  willbecome ..Afterthat,thesumofnnumbers......
  • HDU 4436 str2int
    ProblemDescriptionInthisproblem,youaregivenseveralstringsthatcontainonlydigitsfrom'0'to'9',inclusive.Anexampleisshownbelow.1......
  • HDU 5264 pog loves szh I
    ProblemDescriptionPoghaslotsofstrings.Andhealwaysmixestwoequal-lengthstrings.Forexample,therearetwostrings:"abcd"and"efgh".Afterm......
  • HDU 5639 Deletion
    ProblemDescriptionG with n verticesand m edges.Everytime,youcanselectseveraledgesanddeletethem.Theedgesselectedmustmeetthe......
  • HDU 5637 Transform
    ProblemDescriptionn integersaregiven.Foraninteger x youcandothefollowingoperations:+letthebinaryrepresentationof x be ......