HDU-4041-Eliminate Witches! (11年北京网络赛!!)
Eliminate Witches!
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1124 Accepted Submission(s): 426
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:
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.
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 ','.
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.
3
walpurgis(charlotte(patricia,gertrud),elly,gisela)
wuzetian
nanoha(fate(hayate))
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 <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年北京网络赛!!)的更多相关文章
- HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011亚洲北京赛区网络赛)
HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011 亚洲北京赛区网络赛题目) Eliminate Witches! Time Limit: 2000/1000 ...
- HDU 4041 Eliminate Witches! --模拟
题意: 给一个字符串,表示一颗树,要求你把它整理出来,节点从1开始编号,还要输出树边. 解法: 模拟即可.因为由括号,所以可以递归地求,用map存对应关系,np存ind->name的映射,每进入 ...
- hihocoder1236(北京网络赛J):scores 分块+bitset
北京网络赛的题- -.当时没思路,听大神们说是分块+bitset,想了一下发现确实可做,就试了一下,T了好多次终于过了 题意: 初始有n个人,每个人有五种能力值,现在有q个查询,每次查询给五个数代表查 ...
- 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 ...
- 2015北京网络赛 J Scores bitset+分块
2015北京网络赛 J Scores 题意:50000组5维数据,50000个询问,问有多少组每一维都不大于询问的数据 思路:赛时没有思路,后来看解题报告也因为智商太低看了半天看不懂.bitset之前 ...
- 2015北京网络赛 Couple Trees 倍增算法
2015北京网络赛 Couple Trees 题意:两棵树,求不同树上两个节点的最近公共祖先 思路:比赛时看过的队伍不是很多,没有仔细想.今天补题才发现有个 倍增算法,自己竟然不知道. 解法来自 q ...
- HDU 4759 Poker Shuffle(2013长春网络赛1001题)
Poker Shuffle Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
- HDU 5875 Function -2016 ICPC 大连赛区网络赛
题目链接 网络赛的水实在太深,这场居然没出线zzz,差了一点点,看到这道题的的时候就剩半个小时了.上面是官方的题意题解,打完了才知道暴力就可以过,暴力我们当时是想出来了的,如果稍稍再优化一下估计就过了 ...
- HDU 4763 Theme Section (2013长春网络赛1005,KMP)
Theme Section Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Tot ...
随机推荐
- ubuntu14.04下 Android虚拟机 genymotion 的下载和安装
官网:https://www.genymotion.com/ Install Guide https://www.genymotion.com/#!/developers/user-guide#ins ...
- Windows下配置g++的简单方法
需要下载名为 Cygnus的软件.下载地址: http://www.claremontmckenna.edu/math/ALee/g++/full.exe 安装完成后配好环境变量就可以用gcc, g+ ...
- service httpd restart失败解决方法(小记)
1.首先查看错误日志 /var/log/message看看具体问题.如果一时难以判定直接使用下面一招 2.直接中断http服务,killall -9 httpd, 然后重启http,service ...
- hexdump——Linux系统的二进制文件查看工具
hexdump是Linux下的一个二进制文件查看工具,可以将二进制文件转换为ASCII.10进制.16进制或8进制进行查看. 首先我们准备一个测试用的文件test,十六进制如下: 00 01 02 0 ...
- Encoding 类别
Encoding 類別 .NET Framework 4.5 表示字元編碼方式. 繼承階層架構 System.Object System.Text.Encoding System.Tex ...
- King's Quest
poj1904:http://poj.org/problem?id=1904 题意:国王有n个儿子,现在这n个儿子要在n个女孩里选择自己喜欢的,有的儿子可能喜欢多个,最后国王的向导给出他一个匹配,匹配 ...
- DJANGO中获取登陆用名及别名
练练,标准认证的. VIEW中导入: from django.contrib.auth.models import User TEMPLATE中可引用: 列表 {{ user.username }}{ ...
- Watch gcc at ubuntu 12,See ELF file header
first write article at my ubuntu 12. ELF is very important file format.
- Spring MVC 解读——@RequestMapping (1)(转)
转自:http://my.oschina.net/HeliosFly/blog/212329 Spring MVC 解读——@RequestMapping 为了降低文章篇幅,使得文章更目标化,简洁化, ...
- 精通iOS开发(第5版)
<精通iOS开发(第5版)> 基本信息 原书名:Beginning ios 6 development:exploring the ios sdk 作者: (美)David Mark ...