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:
 

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.
 

 
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
 
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);   /* 访问右子树 */ 
                           } 
                        }

b)非递归方式 
                  void preorder_nonrecursive(Bitree T)      /* 先序遍历二叉树的非递归算法 */ 
                        { 
                           initstack(S); 
                           push(S,T);             /* 根指针进栈 */ 
                           while(!stackempty(S)) { 
                              while(gettop(S,p)&&p) {      /* 向左走到尽头 */ 
                                 visit(p);      /* 每向前走一步都访问当前结点 */ 
                                 push(S,p->lchild); 
                              } 
                              pop(S,p); 
                              if(!stackempty(S)) {      /* 向右走一步 */ 
                                 pop(S,p); 
                                 push(S,p->rchild); 
                              } 
                           } 
                        }

2)中序遍历

a)递归方式 
                  void inorder_recursive(Bitree T)      /* 中序遍历二叉树的递归算法 */ 
                        { 
                           if (T) { 
                              inorder_recursive(T->lchild);   /* 访问左子树 */ 
                              visit(T);          /* 访问当前结点 */ 
                              inorder_recursive(T->rchild);   /* 访问右子树 */ 
                           } 
                        }

b)非递归方式 
                  void inorder_nonrecursive(Bitree T) 
                        { 
                           initstack(S);            /* 初始化栈 */ 
                           push(S, T);            /* 根指针入栈 */

while (!stackempty(S)) {          
                              while (gettop(S, p) && p)    /* 向左走到尽头 */ 
                                 push(S, p->lchild); 
                              pop(S, p);         /* 空指针退栈 */ 
                              if (!stackempty(S)) { 
                                 pop(S, p); 
                                 visit(p);      /* 访问当前结点 */ 
                                 push(S, p->rchild);   /* 向右走一步 */ 
                              } 
                           } 
                        }

3)后序遍历

a)递归方式 
                  void postorder_recursive(Bitree T)      /* 中序遍历二叉树的递归算法 */ 
                        { 
                           if (T) { 
                              postorder_recursive(T->lchild);   /* 访问左子树 */ 
                              postorder_recursive(T->rchild);   /* 访问右子树 */ 
                              visit(T);             /* 访问当前结点 */ 
                           } 
                        }

b)非递归方式 
                  typedef struct { 
                           BTNode* ptr; 
                           enum {0,1,2} mark; 
                        } PMType;                /* 有mark域的结点指针类型 */

void postorder_nonrecursive(BiTree T)      /* 
                  后续遍历二叉树的非递归算法 */ 
                        { 
                           PMType a; 
                           initstack(S);             /* S的元素为PMType类型 */ 
                           push (S,{T,0});          /* 根结点入栈 */ 
                           while(!stackempty(S)) { 
                              pop(S,a); 
                              switch(a.mark) 
                              { 
                              case 0: 
                                 push(S,{a.ptr,1});    /* 修改mark域 */ 
                                 if(a.ptr->lchild) 
                                    push(S,{a.ptr->lchild,0}); /* 访问左子树 */ 
                                 break; 
                              case 1: 
                                 push(S,{a.ptr,2});    /* 修改mark域 */ 
                                 if(a.ptr->rchild) 
                                    push(S,{a.ptr->rchild,0}); /* 访问右子树 */ 
                                 break; 
                              case 2: 
                                 visit(a.ptr);       /* 访问结点 */ 
                              } 
                           } 
                        }

 

HDU 4041 Eliminate Witches! (模拟题 ACM ICPC 2011亚洲北京赛区网络赛)的更多相关文章

  1. HDU 4041 Eliminate Witches! --模拟

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

  2. HDU 4046 Panda (ACM ICPC 2011北京赛区网络赛)

    HDU 4046 Panda (ACM ICPC 2011北京赛区网络赛) Panda Time Limit: 10000/4000 MS (Java/Others)    Memory Limit: ...

  3. hdu 4041 2011北京赛区网络赛B 搜索 ***

    直接在字符串上搜索,注意逗号的处理 #include<cstdio> #include<iostream> #include<algorithm> #include ...

  4. hdu 4046 2011北京赛区网络赛G 线段树 ***

    还带这么做的,卧槽,15分钟就被A了的题,居然没搞出来 若某位是1,则前两个为wb,这位就是w #include<cstdio> #include<cstring> #defi ...

  5. hdu 4044 2011北京赛区网络赛E 树形dp ****

    专题训练 #include<stdio.h> #include<iostream> #include<string.h> #include<algorithm ...

  6. hdu 4050 2011北京赛区网络赛K 概率dp ***

    题目:给出1-n连续的方格,从0开始,每一个格子有4个状态,左右脚交替,向右跳,而且每一步的步长必须在给定的区间之内.当跳出n个格子或者没有格子可以跳的时候就结束了,求出游戏的期望步数 0:表示不能到 ...

  7. hdu 4049 2011北京赛区网络赛J 状压dp ***

    cl少用在for循环里 #include<cstdio> #include<iostream> #include<algorithm> #include<cs ...

  8. hdu 4045 2011北京赛区网络赛F 组合数+斯特林数 ***

    插板法基础知识 斯特林数见百科 #include<iostream> #include<cmath> #include<cstdio> #include<cs ...

  9. hdu 4043 2011北京赛区网络赛D 概率+大数 **

    推出公式为:P = A(2n,n)/(2^(2n)*n!) 但是不会大数,学完java再补

随机推荐

  1. C陷阱与缺陷(四)

    第四章 连接 4.1 什么是连接器 C语言中的一个重要思想就是分别编译,即若干个源程序可以在不同的时候单独进行编译,然后在恰当的时候整合在一起.典型的连接器把由编译器或汇编器生成的若干个目标模块,整合 ...

  2. WebRTC–getUserMedia-filter

    示例说明:抓取MediaStream的一帧数据,并对该帧数据使用Css滤镜效果. 步骤: 1. 由getUserMedia方法获取一个可用的MediaStream 2. canvas方法drawIma ...

  3. 自己写的一个简单的Tab类

    //------------- PS_DOM 功能函数 start----------------var PS_DOM ={ indexOf: function(arr, e){ for(var i= ...

  4. genymotion下载出现Unable to create virtual device,Server returned HTTP status code 0.

    解决方法:

  5. MFC消息顺序

    1.AfxWndProc()      该函数负责接收消息,找到消息所属的CWnd对象,然后调用AfxCallWndProc 2.AfxCallWndProc()  该函数负责保存消息(保存的内容主要 ...

  6. 和S5933比较起来,开发PLX9054比较不幸,可能是第一次开发PCI的缘故吧。因为,很多PCI的例子都是对S5933,就连微软出版的《Programming the Microsoft Windows Driver Model》都提供了一个完整的S5933的例子。 在这篇有关DDK的开发论文里。

    和S5933比较起来,开发PLX9054比较不幸,可能是第一次开发PCI的缘故吧.因为,很多PCI的例子都是对S5933,就连微软出版的<Programming the Microsoft Wi ...

  7. Python 中的list小结

    list的下标和子list list的下表从零开始,和C语言挺类似的,但是增加了负下标的使用. -len-----第一个元素 ......       ...... -2 ------ 倒数第二个元素 ...

  8. 从零开始学C++之IO流类库(四):输出流格式化(以操纵子方式格式化 以ios类成员函数方式格式化)

    一.以操纵子方式格式化 数据输入输出的格式控制使用系统头文件<iomanip>中提供的操纵符.把它们作为插入操作符<<的输出对象即可.如setiosflags.setw.set ...

  9. Ajax辅助方法

    目前为止,我们已经考察了如何编写客户端JavaScript代码,以发送并接受服务器的数据.然而,在使用ASP.NET MVC时,还有另一种方法可用来执行Ajax调用,这就是Ajax辅助方法. Ajax ...

  10. 全球在一个 level 上思考的价值观和想法是一样的(转)

    近日,福布斯中文版总编辑周建工对话马云,谈到腾讯频繁的大笔收购,马云点评称腾讯收购的所有的案子,老百性都看得懂,这就错了.战略就像买股票一样,如果老太太都开始买股票了,一定有问题. 以下是对话内容,转 ...