当前位置:编程学习 > C/C++ >>

HDU 4041 Eliminate Witches! (ACM ICPC 2011北京赛区网络赛)

HDU 4041 Eliminate Witches! (ACM ICPC 2011 北京赛区网络赛题目)
 
Eliminate Witches!
 
Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 863    Accepted Submission(s): 342
 
 
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
walpurgis(charlotte(patricia,gertrud),elly,gisela) 
wuzetian 
nanoha(fate(hayate)) 
 
 
Sample Output
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 
 
wuzetian 
 
nanoha 
fate 
hayate 
1 2 
2 3 
3 2 
2 1
 
 
Source
The 36th ACM/ICPC Asia Regional Beijing Site —— Online Contest
 
 
Recommend
lcy
 
 
 
题目大意:T组测试数据,接下来T组字符串,表示一棵树,从字符串看树的结构显而易见,然后输出这棵树访问的过程。
 
解题思路:模拟题,这题就是一种先序的方式,但是不是二叉树,所以自己写程序模拟一下过程就可以了
#include <iostream>
#include <cstring>
#include <cstdio>
#include <string>
#include <map>
#include <cstring>
#include <vector>
using namespace std;

const int maxlen=10000010;
vector <string> ans;
vector <int> v;
map <string,int> mp;
char str[maxlen];
int len,num,pos;

void initial(){
	ans.clear();
	mp.clear();
	v.clear();
	num=1;
	pos=0;
}

void input(){
	scanf("%s",str);
	len=strlen(str);
}

void dfs(){
	if(pos>=len) return;
	string st;
	while(str[pos]>='a' && str[pos]<= 'z'){
		st.push_back(str[pos]);
		pos++;
	}
	ans.push_back(st);
	int now=num++;
	v.push_back(now);
	if(str[pos]=='('){
		pos++;
		dfs();
		v.push_back(now);
		while(str[pos]==','){
			pos++;
			dfs();
			v.push_back(now);
		}
		pos++;
	}else return;
}

void computing(){
	dfs();
}

void output(){
	printf("%d\n",num-1);
	for(int i=0;i<ans.size();i++){
		printf("%s\n",ans[i].c_str());
	} 
	for(int i=0;i<v.size()-1;i++){
		printf("%d %d\n",v[i],v[i+1]);
	}
	printf("\n");
}


int main(){
	int t;
	cin>>t;
	while(t-- >0){
		initial();
		input();
		computing();
		output();
	}
	return 0;
}

 

拓展:树的遍历方式(转载)
            1)前序遍历
            a)递归方式: 
                  void preorder_recursive(Bitree T)      /* 先序遍历二叉树的递归算法 */ 
                        { 
                           if (T) { 
                              visit(T);          /* 访问当前结点 */ 
                              preorder_recursive(T->lchild);   /* 访问左子树 */ 
                              preorder_recursive(T->rchild
补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,