首页 > 代码库 > hdu 1022 Train Problem I(stack)

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.\n");        }        else        {            printf("Yes.\n");            for(i=0;i<flag;i++)            {                if(ans[i]==1) printf("in\n");                else if(ans[i]==2) printf("out\n");            }        }        printf("FINISH\n");    }    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 0x7ffffffusing 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.\n");        else        {            cout<<"Yes."<<endl;            for(i=0;i<flag;i++)            {                if(vis[i]==1) printf("in\n");                else if(vis[i]==2) printf("out\n");            }        }        printf("FINISH\n");    }    return 0;}

 

hdu 1022 Train Problem I(stack)