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;
}

版权声明:本文为博主原创文章,未经博主允许不得转载。http://xiang578.top/

posted @ 2015-04-18 14:39  xryz  阅读(140)  评论(0编辑  收藏  举报