HDU1022 Train Problem I

简介:
题目地址: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,如需转载请自行联系原作者
目录
相关文章
|
9月前
UVa1531 - Problem Bee
UVa1531 - Problem Bee
34 0
|
6月前
|
Java
hdu 1022 Train Problem I【模拟出入栈】
hdu 1022 Train Problem I【模拟出入栈】
24 0
hdu 1022 Train Problem I【模拟出入栈】
HDOJ(HDU) 1898 Sempr == The Best Problem Solver?(水题、、、)
HDOJ(HDU) 1898 Sempr == The Best Problem Solver?(水题、、、)
100 0
|
C++
leetcode 37. Sudoku Solver 36. Valid Sudoku 数独问题
三星机试也考了类似的题目,只不过是要针对给出的数独修改其中三个错误数字,总过10个测试用例只过了3个与世界500强无缘了 36. Valid Sudoku Determine if a Sudoku is valid, according to: Sudoku Puzzles - The Rules.
1182 0