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

HDUOJ-------(1022)Train Problem I

2013-10-13 20:59 459 查看

Train Problem I

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 16859 Accepted Submission(s): 6300

[align=left]Problem Description[/align]
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<cstdio>
#include<stack>
using namespace std;
int main()
{
int n,i;
while(cin>>n)
{

stack<char> s;
bool c[24];
int rank=0,step=0;
char a[12],b[12];
scanf("%s %s",b,a);
for(i=0;i<n;i++)
{
if(a[rank]==b[i])
{
/*false---》进栈,true---》出栈*/
c[step++]=false;
c[step++]=true;
rank++;
}
else
{
while(!s.empty())
{
if(a[rank]==s.top())
{
rank++;
s.pop();
c[step++]=true;
}
else break;
}
s.push(b[i]);
c[step++]=false;
}
}
while(rank<n&&a[rank]==s.top())
{
rank++;
s.pop();
c[step++]=true;
}
if(!s.empty())
{
printf("No.\n");
}
else
{
puts("Yes.");
for(i=0;i<2*n;i++)
{
if(c[i])
puts("out");
else
puts("in");
}
}
puts("FINISH");
}
return 0;
}


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