题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1022
#include <iostream>
#include <string>
#include <stack>
#include <vector>
using namespace std;
string strIn,strOut;
stack<char> strTmp;//临时栈
vector<string> strInfo;
int main(int argc,char* argv[])
{
int n,i,curPos;
char ch;
while(cin>>n)
{
cin>>strIn>>strOut;
curPos = 0;//out指针
while(!strTmp.empty())
{
strTmp.pop();
}
while(strInfo.size()!=0)
{
strInfo.pop_back();
}
for(i=0;i<strIn.length();++i)
{
if(strTmp.empty()==false)
{//栈不空
ch = strTmp.top();
while(strOut[curPos]==ch&&(strTmp.empty()==false))
{//相等,出栈
strTmp.pop();
strInfo.push_back("out");
curPos++;//当前指针后移
if(strTmp.empty())
{
break;
}
ch = strTmp.top();
}
strTmp.push(strIn[i]);
strInfo.push_back("in");
}
else
{//栈空
strTmp.push(strIn[i]);//入栈
strInfo.push_back("in");
}
}
while(!strTmp.empty())
{
ch = strTmp.top();
strInfo.push_back("out");
if(ch!=strOut[curPos])
{
cout<<"No."<<endl;
break;
}
strTmp.pop();
curPos++;
}
if(strTmp.empty())
{
cout<<"Yes."<<endl;
for(i=0;i<strInfo.size();++i)
{
cout<<strInfo[i]<<endl;
}
}
cout<<"FINISH"<<endl;
}
return 0;
}
本文转自Phinecos(洞庭散人)博客园博客,原文链接:http://www.cnblogs.com/phinecos/archive/2008/01/06/1027870.html,如需转载请自行联系原作者