您的位置:首页 > 大数据 > 人工智能

杭电1022Train Problem I

2016-04-04 15:19 483 查看
地址:http://acm.hdu.edu.cn/showproblem.php?pid=1022 题目: Problem Description As the new term comes, the Ignatius Train Station is very busy nowadays. A lot of student want to get back to school by train(because the trains in the Ignatius Train Station is the fastest all over the world ^v^). But here comes a problem, there is only one railway where all the trains stop. So all the trains come in from one side and get out from the other side. For this problem, if train A gets into the railway first, and then train B gets into the railway before train A leaves, train A can't leave until train B leaves. The pictures below figure out the problem. Now the problem for you is, there are at most 9 trains in the station, all the trains has an ID(numbered from 1 to n), the trains get into the railway in an order O1, your task is to determine whether the trains can get out in an order O2.
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#include <string>

#define PI acos((double)-1)
#define E exp(double(1))
#define N 1000000
using namespace std;

int main (void)
{
int t,a,b,flag,c;
string s,e,code;
stack<int>sk;
while(scanf("%d",&t) == 1 && t)
{
cin>>s>>e;
flag=1;
a=b=c=0;
while(b<t)
{
if(s[a] == e[b])
{
a++;b++;
code[c++]=1;
code[c++]=0;
}
else if(!sk.empty() && e[b] == sk.top())
{
sk.pop();b++;
code[c++]=0;
}
else if(a < t)
{
sk.push(s[a++]);
code[c++]=1;
}
else
{
flag = 0;
break;
}
}
if(flag)
{
cout<<"Yes."<<endl;
for(int i = 0;i< 2*t;i++)
if(code[i])
cout<<"in"<<endl;
else
cout<<"out"<<endl;
}
else
cout<<"No."<<endl;
cout<<"FINISH"<<endl;
while(!sk.empty())
sk.pop();
}
return 0;
}
View Code

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: