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

杭电hdu 1022 Train Problem I 栈

2012-04-18 09:14 417 查看
http://acm.hdu.edu.cn/showproblem.php?pid=1022

栈的简单实现。

#include <iostream>

using namespace std;

#include <string>
#include <queue>
#include <stack>

queue<string >Q;
stack<char >S;

int main()
{
int n;
char train1[10], train2[10], tmp;
int i, j;
while(scanf("%d", &n)!=EOF){
scanf("%s%s", train1, train2);
//		puts(train1);
//		puts(train2);
S.push(train1[0]);
Q.push("in");
for(i = 1, j = 0; j < n;){

if(S.empty()&&i < n){
S.push(train1[i]);
Q.push("in");
i ++;
}
tmp = S.top();
//			printf("%c\n", tmp);
if(tmp == train2[j]){
S.pop();
Q.push("out");
j ++;
}
else{
if(i < n){
S.push(train1[i]);
Q.push("in");
i ++;
}
else break;
}
}
if(i >= n && j >= n){
printf("Yes.\n");
while(!Q.empty()){
string s = Q.front();
Q.pop();
cout<<s<<endl;
}
}
else {
printf("No.\n");
}
printf("FINISH\n");
while(!Q.empty())Q.pop();
while(!S.empty())S.pop();
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: