hdu 4041 Eliminate Witches! 栈和队列

/* ***********************************************
Author        :xryz
Email         :523689985@qq.com
Created Time  :4-17 20:52:26
File Name     :Eliminate Witches!.cpp
************************************************ */

#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <math.h>
#include <stdlib.h>
#include <stack>
#include <time.h>
using namespace std;
struct node
{
    char str[15];
} n[50001];
queue<int>q;
stack<int>s;

int main()
{
    int T,i,j,cnt,len;
    char name[16],ss[1000001];
    scanf("%d",&T);
    while(T--)
    {
        while(!q.empty()) q.pop();
        while(!s.empty()) s.pop();
        scanf("%s",ss);
        cnt=0;
        len=strlen(ss);
        j=0;
        for(i=0;i<len;i++)
        {
            if(ss[i]>='a'&&ss[i]<='z')
                name[j++]=ss[i];
            else
            {
                if(j!=0)
                    {
                        name[j]=0;
                        j=0;
                        strcpy(n[++cnt].str,name);
                    }
                if(ss[i]=='(')
                {
                    q.push(cnt);
                    s.push(cnt);
                }
                else if(ss[i]==',')
                {
                    if(ss[i-1]!=')')
                    {
                        q.push(cnt);
                        q.push(s.top());
                        //s.pop();
                    }
                    else q.push(s.top());
                }
                else if(ss[i]==')')
                {
                    if(ss[i-1]!=')')
                    {
                        q.push(cnt);
                        q.push(s.top());
                        s.pop();
                    }
                    else
                    {
                        q.push(s.top());
                        s.pop();
                    }
                }
            }
        }
        if(cnt==0)
        {
            printf("1\n%s\n",ss);
        }
        else
        {
            printf("%d\n",cnt);
            for(i=1; i<=cnt; i++)
                printf("%s\n",n[i].str);
            while(!q.empty())
            {
                if(q.front()==1&&q.size()==1) break;
                printf("%d ",q.front());
                q.pop();
                printf("%d\n",q.front());
            }
        }
    printf("\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值