快速解決HDU1022問題

         詳細問題見鏈接: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;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章