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 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
 
Source
 

网络赛模拟题,做来耍耍

思路:模拟树的先序遍历过程!

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


代码:


#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;
}


HDU-4041-Eliminate Witches! (11年北京网络赛!!)的更多相关文章

  1. HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011亚洲北京赛区网络赛)

    HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011 亚洲北京赛区网络赛题目) Eliminate Witches! Time Limit: 2000/1000 ...

  2. HDU 4041 Eliminate Witches! --模拟

    题意: 给一个字符串,表示一颗树,要求你把它整理出来,节点从1开始编号,还要输出树边. 解法: 模拟即可.因为由括号,所以可以递归地求,用map存对应关系,np存ind->name的映射,每进入 ...

  3. hihocoder1236(北京网络赛J):scores 分块+bitset

    北京网络赛的题- -.当时没思路,听大神们说是分块+bitset,想了一下发现确实可做,就试了一下,T了好多次终于过了 题意: 初始有n个人,每个人有五种能力值,现在有q个查询,每次查询给五个数代表查 ...

  4. 2015北京网络赛 D-The Celebration of Rabbits 动归+FWT

    2015北京网络赛 D-The Celebration of Rabbits 题意: 给定四个正整数n, m, L, R (1≤n,m,L,R≤1000). 设a为一个长度为2n+1的序列. 设f(x ...

  5. 2015北京网络赛 J Scores bitset+分块

    2015北京网络赛 J Scores 题意:50000组5维数据,50000个询问,问有多少组每一维都不大于询问的数据 思路:赛时没有思路,后来看解题报告也因为智商太低看了半天看不懂.bitset之前 ...

  6. 2015北京网络赛 Couple Trees 倍增算法

    2015北京网络赛 Couple Trees 题意:两棵树,求不同树上两个节点的最近公共祖先 思路:比赛时看过的队伍不是很多,没有仔细想.今天补题才发现有个 倍增算法,自己竟然不知道.  解法来自 q ...

  7. HDU 4759 Poker Shuffle(2013长春网络赛1001题)

    Poker Shuffle Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

  8. HDU 5875 Function -2016 ICPC 大连赛区网络赛

    题目链接 网络赛的水实在太深,这场居然没出线zzz,差了一点点,看到这道题的的时候就剩半个小时了.上面是官方的题意题解,打完了才知道暴力就可以过,暴力我们当时是想出来了的,如果稍稍再优化一下估计就过了 ...

  9. HDU 4763 Theme Section (2013长春网络赛1005,KMP)

    Theme Section Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Tot ...

随机推荐

  1. Make body have 100% of the browser height

    Try setting the height of the html element to 100% as well. html, body { height: 100%; } Body looks ...

  2. centos下的lnmp环境搭建

    1.配置centos的第三方yum源,因为原始的yum是无法安装nginx的 wget http://www.atomicorp.com/installers/atomic  下载atomic yum ...

  3. Python 学习之urllib模块---用于发送网络请求,获取数据(2)

    接着上一次的内容. 先说明一下关于split()方法:它通过指定分隔符对字符串进行切片,如果参数num 有指定值,则仅分隔 num 个子字符串(把一个字符串分割成很多字符串组成的list列表) 语法: ...

  4. Python 手册——参数传递以及交互模式

    我们先来看参数传递. 调用解释器时,脚本名和附加参数之传入一个名为sys.argv的字符串列表.没有脚本和参数时,它至少也有一个 元素:sys.argv[0]此时为空字符串.脚本名指定为‘ - ’(表 ...

  5. (转) 基于Theano的深度学习(Deep Learning)框架Keras学习随笔-01-FAQ

    特别棒的一篇文章,仍不住转一下,留着以后需要时阅读 基于Theano的深度学习(Deep Learning)框架Keras学习随笔-01-FAQ

  6. [CF Round #294 div2] E. A and B and Lecture Rooms 【树上倍增】

    题目链接:E. A and B and Lecture Rooms 题目大意 给定一颗节点数10^5的树,有10^5个询问,每次询问树上到xi, yi这两个点距离相等的点有多少个. 题目分析 若 x= ...

  7. 百度统计js被劫持用来DDOS Github

    今天中午刷着全国最大的信息安全从业人员同性交友社区zone.wooyun.org的时候,忽然浏览器每隔2秒就不断的弹窗: malicious javascript detected on this d ...

  8. Reactor Cooling

    sgu194:http://acm.sgu.ru/problem.php?contest=0&problem=194 题意:题目大意:给n个点,及m根pipe,每根pipe用来流躺液体的,单向 ...

  9. Count The Pairs

    hdu4750:http://acm.hdu.edu.cn/showproblem.php?pid=4750 题意:给你一个带权无向图,然后让你求这样的点对s,t,使得s--t的所有路径上的最大的边的 ...

  10. ADI加速度计基础原理