http://acm.hdu.edu.cn/showproblem.php?pid=1022
#pragma warning(disable : 4786)
#include <iostream>
#include <stack>
#include <string>
#include <vector>
using namespace std;
bool run(int now)
{
int n;
if (!(cin>>n)) return false;
string s,t;
cin>>s>>t;
stack<int> st;
vector <string> op;
int i=0, j=0;
while(i<=n && j<n)
{
if (st.empty()==true || st.top()!=t[j])
{
st.push(s[i]);
op.push_back("in");
i++;
}
else
{
st.pop();
op.push_back("out");
j++;
}
}
if (st.empty()==true)
{
cout << "Yes."<<endl;
for(i=0;i<op.size();i++)
{
cout<<op[i]<<endl;
}
}
else
{
cout << "No."<<endl;
}
cout << "FINISH"<<endl;
return true;
}
int main()
{
int now=1;
while(run(now++));
return 0;
}
本文介绍了一个使用栈数据结构来模拟并验证字符串操作的过程。通过输入两个字符串s和t,程序将尝试用一系列的入栈(in)和出栈(out)操作,使得栈顶元素与字符串t匹配。如果成功,则输出Yes及操作序列;反之则输出No。此程序适用于算法学习和栈操作的实践。
1349

被折叠的 条评论
为什么被折叠?



