http://acm.hdu.edu.cn/showproblem.php?pid=1022

题意 :其实我没看题,看了样例还有下边的那个解释,就知道是什么意思了,这个我也说不清楚,,,,只可意会不可言传,反正就是火车按照第一个字符串进站,看能不能按第二个出来,如果能就输出怎么进出的,不能直接输出NO.。

思路 :栈的应用。

#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
using namespace std ;
int main()
{
    stack<char>Q ;
    int n ,flag[100];
    //memset(flag,0,sizeof(flag)) ;
    char ch[110],sh[110] ;
    while(cin>>n>>ch>>sh)
    {
        Q.push(ch[0]) ;//先将第一个入栈是防止下面判断的时候栈空,无法取元素
        flag[0] = 1 ;
        int cnt = 1 ,i = 0,j = 0;
        while(i < n && j < n )
        {
            if(Q.size() && Q.top() == sh[j])
            {
                Q.pop() ;//如果栈顶元素等于第二个字符串的第一个就直接让它出栈
                j++ ;//字符串后移
                flag[cnt++] = 0 ;//因为是出的,标记为0,进的标记为1
            }
            else
            {
                //if(i == n) break ;
                Q.push(ch[++i]) ;//如果不相等就继续入栈
                flag[cnt++] = 1 ;
            }
        }
        if(i == n)//如果i==n了,就代表着已经全入完栈了,却是没有与字符串2相等的了。
            cout<<"No."<<endl<<"FINISH"<<endl;
        else
        {
            cout<<"Yes."<<endl ;
            for(int i = 0 ; i < cnt ; i++)
            {
                if(flag[i] == 1)
                    cout<<"in"<<endl ;
                else
                    cout<<"out"<<endl ;
            }
            cout<<"FINISH"<<endl ;
        }
    }
    return 0 ;
}
View Code

 

posted on 2013-11-24 17:02  枫、  阅读(215)  评论(0编辑  收藏  举报