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

HDU 1022 Train Problem I

2015-01-26 14:27 351 查看
这题我的想法使用栈的思想去解决的,就是根据进站的序列加入栈中,若和出站最开始的序号相同,则出栈;然后继续进行此操作,和下一个出站的火车序号相比较。这样操作完后看看栈是否为空,就可以判断了。

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<stack>
#include<queue>
using namespace std;
int main()
{
char s1[10],s2[10],s3[20][5];
int n;
while(scanf("%d%s%s",&n,s1,s2)!=EOF)
{
stack<char> s;
int top2=0,top3=0;
for(int i=0;i<n;i++)
{
s.push(s1[i]);
strcpy(s3[top3++],"in");//此处就是我的思想
while(!s.empty())
{
char a=s.top();
if(a==s2[top2])
{
strcpy(s3[top3++],"out");
s.pop();
top2++;
}
else
break;
}
}
if(s.empty())
{
printf("Yes.\n");
for(int i=0;i<top3;i++)
printf("%s\n",s3[i]);
}
else
printf("No.\n");
printf("FINISH\n");
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: