• 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
    %s
    ",ss);
            }
            else
            {
                printf("%d
    ",cnt);
                for(i=1; i<=cnt; i++)
                    printf("%s
    ",n[i].str);
                while(!q.empty())
                {
                    if(q.front()==1&&q.size()==1) break;
                    printf("%d ",q.front());
                    q.pop();
                    printf("%d
    ",q.front());
                }
            }
        printf("
    ");
        }
        return 0;
    }
    

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

  • 相关阅读:
    JAVA多线程与并发学习总结
    Java虚拟机类加载机制
    2013网易校园招聘笔试题
    人人网面试题
    2010人人网校园招聘笔试题
    2011人人网校园招聘笔试题
    2012人人网校园招聘笔试题
    2013人人网校园招聘笔试题
    海量数据查找唯一数据问题
    Hulu面试题
  • 原文地址:https://www.cnblogs.com/xryz/p/4848037.html
Copyright © 2020-2023  润新知