首页 技术 正文
技术 2022年11月17日
0 收藏 397 点赞 3,817 浏览 3785 个字

Eliminate Witches!

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 1124    Accepted Submission(s): 426

Problem DescriptionKaname 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. 
 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 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 ‘,’. 
 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 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

 SourceThe 36th ACM/ICPC Asia Regional
Beijing Site —— Online Contest
 
网络赛模拟题,做来耍耍
思路:模拟树的先序遍历过程!

。但注意这不是二叉树!。用栈和队列实现就可以!!

代码:

#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());
}
}
putchar(10); //记得换行!!
}
return 0;
}

相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,105
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,582
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,429
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,200
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,836
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,919