您的位置:首页 > 大数据 > 人工智能

hdu 1022 Train Problem I_栈的应用经典例题

2014-07-14 16:21 501 查看
#include <iostream>
#include <stack>
#include <cstring>

using namespace std;

int main()
{
    stack<char>s1;
    stack<char>s2;
    int n,i,j,k,a[10000];
    char str1[100010],str2[100010];
    while(cin>>n)
    {
        memset(str1,0,sizeof(str1));
        memset(str2,0,sizeof(str2));

        cin>>str1;
        cin>>str2;
        for(i=strlen(str2)-1; i>=0; i--)
            s2.push(str2[i]);
        for(i=0,j=0; i<strlen(str1); i++)
        {
            s1.push(str1[i]);
            a[j++]=1;
            while(!s1.empty()&&s1.top()==s2.top())
            {
                s1.pop();
                s2.pop();
                a[j++]=0;
            }
        }
        if(s1.empty()&&s2.empty())
        {
            cout<<"Yes."<<endl;
            for(i=0; i<2*n; i++)
                if(a[i]==1)
                    cout<<"in"<<endl;
                else
                    cout<<"out"<<endl;
        }

        else
        {
            cout<<"No."<<endl;
            while(!s1.empty())
                s1.pop();
            while(!s2.empty())
                s2.pop();
        }

        cout<<"FINISH"<<endl;
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: