码迷,mamicode.com
首页 > 其他好文 > 详细

hdu_1022_Train Problem I_(模拟)

时间:2014-12-22 09:33:55      阅读:156      评论:0      收藏:0      [点我收藏+]

标签:

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1022

题意:有n辆火车,给出入栈和出栈的顺序,编写程序判段出栈是否正确。

样例:3 123 132 是可以的

<span style="font-size:18px;">#include <iostream>
#include <stack> 
#include <cstring>
using namespace std;
int main(int argc, char *argv[])
{
    int n;
    char in[10],out[10];
    stack<char> s;
    int sign[20];//尽量大点,标记:1代表入栈,0代表出栈
    while(cin >> n >> in >> out)
    {
    	memset(sign,0,sizeof(sign));
    	//保证测试每组数据前都是空栈 
        while(!s.empty())
        {
            s.pop();
        }
        int j = 0,k = 0;
        for(int i = 0;i < n;i++)
        {
            s.push(in[i]);
            sign[k++] = 1;
            while(!s.empty() && j < n)//空栈和出栈顺序访问完了 
            {
                if(s.top() != out[j])
                {
                    break;
                }
                else
                {
                    s.pop();
                    sign[k++] = 0;
                    j++;
                }    
            }
        }
        if(s.empty())
        {
            cout << "Yes." << endl;
            for(int i = 0;i < k;i++)//i < n会WA ,k是 “in” 和 “out ” 的总次数 
            {
             	if(sign[i] == 1)
				 	cout << "in" << endl;
				else
					 cout << "out" << endl;
            }
            cout << "FINISH" << endl;    
        }    
        else
            cout << "No." << endl << "FINISH" << endl;    
    }
    return 0;
}</span>



hdu_1022_Train Problem I_(模拟)

标签:

原文地址:http://blog.csdn.net/lyn12030706/article/details/42065619

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!