zoukankan      html  css  js  c++  java
  • 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/

  • 相关阅读:
    java9的JShell小工具和编译器两种自动优化
    运算符
    数据类型
    常量&&变量
    强化学习复习笔记
    强化学习复习笔记
    DQN算法原理详解
    语言模型评价指标Perplexity
    C++ STL标准容器插入删除算法的复杂度
    集束搜索beam search和贪心搜索greedy search
  • 原文地址:https://www.cnblogs.com/xryz/p/4848037.html
Copyright © 2011-2022 走看看