All in All UVA - 10340

本文介绍了一种使用栈进行字符串匹配的算法。通过两个栈分别存储两个输入字符串,然后逐个比较并弹出相同字符,最终判断第一个字符串是否完全匹配第二个字符串。这是一种简单而有效的字符串匹配方法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>
#include <string>
#include <stack>

using namespace std;

int main() {
	string s, t;
	stack<char> st1, st2;
	while (cin >> s >> t) {
		for (int i = 0; i < s.length(); i++)
			st1.push(s[i]);
		for (int i = 0; i < t.length(); i++)
			st2.push(t[i]);
		while (!st1.empty() && !st2.empty()) {
			if (st1.top() == st2.top()) {
				st1.pop();
				st2.pop();
			}
			else
				st2.pop();
		}
		if (!st1.empty())
			cout << "No" << endl;
		else
			cout << "Yes" << endl;
		while (!st1.empty())
			st1.pop();
		while (!st2.empty())
			st2.pop();
	}
	return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值