Eliminate Witches! [2011 北京网络赛]

B  Eliminate Witches!
Accept:340     Submit:1057
Time Limit:1000MS     Memory Limit:65536KB
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


最后一个样例也需要换行,没注意,PE了两次~~~ 大哭

思路:
把字符串 walpurgis(charlotte(patricia,gertrud),elly,gisela) 转换成1(2(34)56)
如果是存到char数组处理不是很方便,因此把"("和")"转换成数字,因为最多只有5W个room,
所以用    0x1ffff表示"(",   x2ffff表示")"
存到int数组就可以了~
最后就是int数组的遍历问题了,主要是以输出后就回退为主(1 2 ;2 1),
再处理一下细节就可以了,以1(2(34)56)举例~
1、如果当前是"("即0x1ffff,则前一个数入栈
2、如果当前是")"即0x2ffff,则出栈,如果栈中元素大于等于2个就回退输出,
如:当前是第一个")",就回退输出2 1,此时栈中就剩下一个1
3、如果是其它,判断栈是否为空,不空则输出
判断下一个数是否为"("即0x1ffff,不是则回退~

include<cmath>
#include<algorithm>
#include<vector>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<string>
#include<stack>
using namespace std;

const int start=0x1ffff;
const int end=0x2ffff;

struct Node
{
    char name[13];
} node[50005];
char mystr[1000005];
int dstr[1000005];
inline void print(int a,int b)
{
    printf("%d %d\n",a,b);
}
int main()
{
    char str[13];
    int T,top,tol,dtol;
    scanf("%d",&T);
    stack<int>s;
    for(int ii=1; ii<=T; ii++)
    {
        scanf("%s",mystr);
        int len=strlen(mystr);
        top=0,tol=1,dtol=0;
        while(!s.empty())
            s.pop();
        for(int i=0; i<len; i++)
        {
            if(mystr[i]=='(')
            {
                if(!(mystr[i-1]=='('||mystr[i-1]==')'||mystr[i-1]==','))
                {
                    str[top]='\0';
                    strcpy(node[tol].name,str);
                    top=0;

                    dstr[dtol++]=tol++;
                }
                dstr[dtol++]=start;
            }
            else if(mystr[i]==')')
            {
                if(!(mystr[i-1]=='('||mystr[i-1]==')'||mystr[i-1]==','))
                {
                    str[top]='\0';
                    strcpy(node[tol].name,str);
                    top=0;

                    dstr[dtol++]=tol++;
                }
                dstr[dtol++]=end;
            }
            else if(mystr[i]==',')
            {
                if(!(mystr[i-1]=='('||mystr[i-1]==')'||mystr[i-1]==','))
                {
                    str[top]='\0';
                    strcpy(node[tol].name,str);
                    top=0;

                    dstr[dtol++]=tol++;
                }
            }
            else
            {
                str[top++]=mystr[i];
                if(i==len-1)
                {
                    str[top]='\0';
                    strcpy(node[tol].name,str);

                    dstr[dtol++]=tol++;
                }
            }
        }
//        for(int i=0; i<dtol; i++)
//            cout<<dstr[i]<<" ";
//        cout<<endl;

        printf("%d\n",tol-1);
        for(int i=1; i<tol; i++)
        {
            printf("%s\n",node[i].name);
        }
        for(int i=0; i<dtol; i++)
        {
            if(dstr[i]==start)
            {
                s.push(dstr[i-1]);
            }
            else if(dstr[i]==end)
            {
                if(s.size()>=2)
                {
                    int tmp=s.top();
                    s.pop();
                    print(tmp,s.top());
                }
            }
            else
            {
                if(!s.empty())
                {
                    print(s.top(),dstr[i]);
                    if(dstr[i+1]!=start)
                        print(dstr[i],s.top());
                }
            }
        }
        printf("\n");
    }
    return 0;
}


评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值