【UVA514】+【HDOJ1022]:出栈序列的可能性问题

UVA514:https://vjudge.net/problem/UVA-514

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

思路:


两个题目都是一个思路,给出待进栈的序列和出栈序列,判断出栈序列是否有可能存在

UVA514是《算法竞赛入门经典》P140的例题,HDOJ1022和它是同样的思路

具体请参见代码,举个栗子模拟一下就明白代码的意思了

 

ac代码:


UVA514

#include <iostream>
#include <cmath>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <stdlib.h>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <sstream>
#define maxn 1005
#define inf 1e+9+10
using namespace std;
typedef long long ll;
int n,target[maxn];
int main()
{
    //freopen("/Users/zhangkanqi/Desktop/11.txt","r",stdin);
    ios::sync_with_stdio(false);//写了这句话的话就不要再用scanf和printf了!!会wa的
    while(cin>>n&&n)
    {
        stack<int> s;
        while(cin>>target[1]&&target[1])
        {
            int a = 1, b = 1;
            for (int i = 2; i <= n; i++)
                cin>>target[i];
            int ok = 1;
            while (b <= n) {
                if (a == target[b]) {
                    a++;
                    b++;
                } else if (!s.empty() && s.top() == target[b]) {
                    s.pop();
                    b++;
                } else if (a <= n)
                    s.push(a++);
                else {
                    ok = 0;
                    break;
                }
            }
            printf("%s\n", ok ? "Yes" : "No");
        }
        printf("\n");
    }
    return 0;
}

HDOJ1022

#include <iostream>
#include <cmath>
#include <cstring>
#include <cctype>
#include <algorithm>
#include <stdlib.h>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#include <sstream>
#define maxn 15
#define inf 1e+9+10
using namespace std;
typedef long long ll;
int n;
char a[maxn],b[maxn];
int main()
{
    //freopen("/Users/zhangkanqi/Desktop/11.txt","r",stdin);
    ios::sync_with_stdio(false);
    int i,j;
    while(cin>>n)
    {
        string ss="";
        for(i=1;i<=n;i++)
            cin>>a[i];
        for(i=1;i<=n;i++)
            cin>>b[i];
        stack<int> s;
        while(!s.empty())//最好写上,如果stack是在前面的while外定义的话,就必须要写!!
            s.pop();
        i=j=1;
        int ok=1;
        while(j<=n)
        {
            if(a[i]==b[j]&& i<=n)//注意!!一定要加上I<=n的判断条件,不然会wa
            {//或者将前面写成cin>>a>>b,就不用加i<=n的判断条件了
                i++;j++;
                ss+="in\nout\n";
            }
            else if(!s.empty()&&s.top()==b[j])
            {
                s.pop();
                j++;
                ss+="out\n";
            }
            else if(a[i]!=b[j]&&i<n)
            {
                s.push(a[i++]);
                ss+="in\n";
            }
            else
            {
                ok=0;
                break;
            }
        }
        if(ok)
        {
            cout<<"Yes."<<endl;
            cout<<ss;
        }
        else
            cout<<"No."<<endl;
        cout<<"FINISH"<<endl;
    }
    return 0;
}

HDOJ1022:同样的思路,另一种写法,用v[i]=1标记in,0标记out,原博主DoubleQ_在代码中自己写了个栈,也可以直接stack<int> s;

#include <stdio.h>
#include <string.h>
int s[10000];
int v[10000];
char in[10000];
char out[10000];
int main()
{
    int n,i,j,k,top;
    while(scanf("%d%s%s",&n,in,out)!=EOF)
    {
        i=0;
        j=0;
        k=0;
        top=-1;
        memset(v,0,sizeof(v));
        while(j<n)
        {
           if(top==-1 || s[top]!=out[j] && i<n)
            {
               s[++top]=in[i++];
                v[k++]=1;
            }
            else
            {
                if(s[top] == out[j])
                {
                    top--;
                     ++j;
                    v[k++]=0;
                }
                else
                break;
            }
        }
        if(top==-1)
        {
           printf("Yes.\n");
            for(i=0;i<k;i++)
            {
                if(v[i])
                printf("in\n");
                else
                printf("out\n");
            }
            printf("FINISH\n");
        }
        else
        printf("No.\nFINISH\n");

    }
        return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值