HDU 1022 栈的简单实现 也可以用 STL 栈 队列

栈的简单实现:

#include <iostream>
using namespace std;
char a[10], b[10], stack[10];
char out[20][4];
int main()
{
	int n;
	while(cin >> n)
	{
		memset(out, 0, sizeof(out));
		cin >> a >> b;
		int i = 0, j= 0, top = 0;
		int t = 0;
		while((stack[top] == b[j] || i != n) && j != n)
		{
			if(b[j] == stack[top])
			{
				j++;
				top--;
				strcpy(out[t++], "out");
			}
			else
			{
				stack[++top] = a[i];
				i++;
				strcpy(out[t++], "in");
			}
		}
		if(t ==  2 * n)
		{
			cout << "Yes." << endl;
			for(int i = 0; i < t; i++)
				cout << out[i] << endl;
			cout << "FINISH" << endl;
		}
		else
		{
			cout << "No." << endl;
			cout << "FINISH" <<endl;
		}
	}
	return 0;
}


STL:

#include <iostream>
#include <queue>
#include <stack>
#include <string>
using namespace std;

const int N = 11;
int n;
int i, j;
char order1[N], order2[N];

void pro()
{
	stack<char> stk;
	queue<string> q;
	for(i = 0, j = 0; i <  n && j <= n;)
	{
		if(stk.empty() || stk.top() != order2[i])
		{
			if(j == n)
			{
				cout << "No.\nFINISH" << endl;
				return;
			}
			stk.push(order1[j++]);
			q.push("in");
		}
		else
		{
			stk.pop();
			q.push("out");
			i++;
		}
	}

	cout << "Yes." << endl;
	while(!q.empty())
	{
		cout << q.front() << endl;
		q.pop();
	}
	cout << "FINISH" << endl;
}

int main()
{
	while(cin >> n)
	{
		cin >> order1 >> order2;
		pro();
	}
	return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值