快速解决HDU1022问题

来源:互联网 发布:人工智能 在线观看 编辑:程序博客网 时间:2024/04/30 03:00

         详细问题见链接:http://acm.hdu.edu.cn/showproblem.php?pid=1022,题意就是输入两个字符串,根据栈操作中的进栈,判断是否能够完成!不能完成打印字符串,能完成打印相关步骤。

        相关代码:

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


int main()
{

int n;

std::vector<string> opt;
string src, dst;
int srcLen, dstLen;
while(cin>>n)
{
if(n <= 0)
{
break;
}


std::stack<char> t1, t2;

cin>>src>>dst;
srcLen = src.length();
dstLen = dst.length();
if(srcLen != dstLen)
{
cout<<"No."<<endl;
cout<<"FINISH"<<endl;
continue;
}


for(int i = srcLen - 1; i >= 0; i--)
{
t1.push(src[i]);
}


int k = 0;
opt.clear();
char data;
while(k < dstLen)
{
while(!t1.empty())
{
data = dst[k];
// if(t1.top() == data)// 栈顶元素刚好等于当前元素 进栈 出栈(实际提交不需要这部分)
// {
// opt.push_back("in");
// opt.push_back("out");
// k++;
// t1.pop();
// continue;
// }


if(!t2.empty() && t2.top() == data)
{
t2.pop();
opt.push_back("out");
k++;
continue;
}

t2.push(t1.top());
t1.pop();
opt.push_back("in");
}


data = dst[k];
if(!t2.empty() && t2.top() == data)
{
t2.pop();
opt.push_back("out");
k++;
continue;
}


break;
}


if(t2.empty())
{
cout<<"Yes."<<endl;
for(std::vector<string>::const_iterator itr = opt.begin(); itr != opt.end(); ++itr)
{
cout<<(*itr)<<endl;
}


cout<<"FINISH"<<endl;
}
else
{
cout<<"No."<<endl;
cout<<"FINISH"<<endl;
}
}


return 0;
}