首页 > 代码库 > hdu_1022_Train Problem I_(模拟)

hdu_1022_Train Problem I_(模拟)

题目链接: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_(模拟)