zoukankan      html  css  js  c++  java
  • hdu 1022 Train Problem I(stack)

    用数组模拟栈

    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    using namespace std;
    char str1[30],str2[30];
    int sta[30];
    int main()
    {
        int n;
        int i,j,k;
        int ans[100];
        while(scanf("%d",&n)!=EOF)
        {
            scanf("%s%s",str1,str2);
            memset(ans,0,sizeof(ans));
    //        memset(vis,0,sizeof(vis));
            int len1=strlen(str1);
            int len2=strlen(str2);
            int l1=0,l2=0,flag=0,l=0,r=0;
            while(l1<=len1)
            {
                if(flag==len1*2) break;
                if(l==r)
                {
                     sta[r++]=str1[l1++];
                     ans[flag++]=1;
                }
                else if(sta[r-1]!=str2[l2])
                {
                    sta[r++]=str1[l1++];
                    ans[flag++]=1;
                }
                else if(sta[r-1]==str2[l2])
                {
                    l2++;
                    r--;
                    ans[flag++]=2;
                }
            }
            if(l<r)
            {
                printf("No.
    ");
            }
            else
            {
                printf("Yes.
    ");
                for(i=0;i<flag;i++)
                {
                    if(ans[i]==1) printf("in
    ");
                    else if(ans[i]==2) printf("out
    ");
                }
            }
            printf("FINISH
    ");
        }
        return 0;
    }

    stl stack

    #include<stdio.h>
    #include<string.h>
    #include<math.h>
    #include<iostream>
    #include<algorithm>
    #include<queue>
    #include<stack>
    #define mem(a,b) memset(a,b,sizeof(a))
    #define ll __int64
    #define MAXN 1000
    #define INF 0x7ffffff
    using namespace std;
    char in[20],out[20];
    int vis[50];
    int main()
    {
        int i,j;
        int n;
        int flag;
        while(scanf("%d",&n)!=EOF)
        {
            int ok=1;
            stack <char> q;
            mem(vis,0);
            flag=0;j=0;i=0;
            scanf("%s%s",in,out);
            while(i<n)
            {
                if(!q.empty()&&q.top()==out[i])
                {
                    q.pop();
                    vis[flag++]=2;
                    i++;
                }
                else if(j<n)
                {
                    vis[flag++]=1;
                    q.push(in[j++]);
                }
                else {ok=0;break;}
            }
            if(!ok) printf("No.
    ");
            else
            {
                cout<<"Yes."<<endl;
                for(i=0;i<flag;i++)
                {
                    if(vis[i]==1) printf("in
    ");
                    else if(vis[i]==2) printf("out
    ");
                }
            }
            printf("FINISH
    ");
        }
        return 0;
    }
  • 相关阅读:
    linux(centos)搭建SVN服务器
    应该具备的能力
    Oracle trunc()函数的用法
    Realistic View for Autodesk Revit 2021
    Snowman
    A Material-Texture Painting Tool
    A Color Picker based on manifold learning
    CPU Path Tracing Renderer
    Rig Space FEM Simulation
    MPM Snow Simulation
  • 原文地址:https://www.cnblogs.com/sola1994/p/4257700.html
Copyright © 2011-2022 走看看