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. 2016022612 - redis事务命令集合

    参考地址:http://www.yiibai.com/redis/redis_transactions.html Redis事务由指令 MULTI 启动,以EXEC结束. 1.multi 用途:事务开 ...

  2. 练习2 F题 - 平方和与立方和

      Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u   Description 给定一 ...

  3. linux源码Makefile的详细分析

    目录 一.概述 1.本文的意义 2.Linux内核Makefile文件组成 二.Linux内核Makefile的“make解析”过程 1 顶层Makefile阶段 1.从总目标uImage说起 2.v ...

  4. babun,windows shell

    babun是windows上的一个第三方shell,在这个shell上面你可以使用几乎所有linux,unix上面的命令,他几乎可以取代windows的shell. babun的几个特点: 使用bab ...

  5. bzoj 1096: [ZJOI2007]仓库建设 斜率優化

    1096: [ZJOI2007]仓库建设 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 2242  Solved: 925[Submit][Statu ...

  6. Contest 20140923 登月计划 BabyStepGaintStep

    登月计划 查看 提交 统计 提问 总时间限制:  40000ms 内存限制:  256000kB 描述 HJA在和学弟学数学,于是便有了一道非常简单的数学题:求满足 的最小自然数x. 输入 输入数据一 ...

  7. java中如何实现全局变量

    有时一个项目中会多处涉及到路径,当你把这个项目移植到别的电脑上时就要一一修改这些路径,过程十分繁琐,所以一个全局变量在这时是必不可少的. 遗憾的是java等oo语言并没有全局变量,这怎么办呢?下面介绍 ...

  8. 在非UI线程中更改UI(Delphi使用隐藏窗口来处理,QT使用信号槽)

    在Delphi里我记得是使用TThread.Synchronize(TThreadMethod),原理是利用了一个隐藏窗口来处理. 在QT Debug模式一下,碰到了同样的问题,显示错误: canno ...

  9. hibernate的速度问题--hibernate.jdbc.fetch_size和 hibernate.jdbc.batch_size

    hibernate的速度问题 这点我也疑惑过,最初应用hibernate的项目,我也感觉速度很慢,知道后来才知道问题的所在.       其实hibernate的速度性能并不差,比起jdbc来说,又是 ...

  10. 3.1日 重温JVM相关信息

    1.JDK.JRE.JVM的关系: JDK是java开发的必备工具箱,JDK其中有一部分是JRE,JRE是JAVA运行环境,JVM则是JRE最核心的部分. 2.JVM的组成: JVM由4大部分组成:C ...