HDU 1022 Train Problem I 類比棧題解,hdu1022

來源:互聯網
上載者:User

HDU 1022 Train Problem I 類比棧題解,hdu1022

火車進站,類比一個棧的操作,額外的棧操作,查看是否能按照規定順序出棧。

資料量很少,故此題目很容易AC。

直接使用數組類比就好。


#include <stdio.h>const int MAX_N = 10;char inOrder[MAX_N], outOrder[MAX_N], stk[MAX_N];bool rs[MAX_N<<2];int n;int main(){while (scanf("%d", &n) != EOF){scanf("%s %s", inOrder, outOrder);int j = 0, out = 0, i = 0, st = 0;bool possible = true;while (possible && !(st == 0 && out == n)){for (; i < n && inOrder[i] != outOrder[out]; i++){rs[j++] = true;stk[st++] = inOrder[i];}//push ini++;//Watch out: don't forget while inOrder[i]==outOrder[out]!rs[j++] = true;rs[j++] = false;out++;while (st > 0 && stk[st-1] == outOrder[out]){st--; out++;rs[j++] = false;}//pop backint k = 0;//check possiblefor (; k < st && stk[k] != outOrder[out]; k++);if (k < st) possible = false;}if (possible){puts("Yes.");for (int i = 0; i < j; i++){if (rs[i]) puts("in");else puts("out");}}else puts("No.");puts("FINISH");}return 0;}

解法二:

#include <stdio.h>const int MAX_N = 10;char inOrder[MAX_N], outOrder[MAX_N], stk[MAX_N];bool rs[MAX_N<<2];int n;int main(){while (scanf("%d", &n) != EOF){scanf("%s %s", inOrder, outOrder);int j = 0, out = 0, i = 0, st = 0;while (i<n && !(st == 0 && out == n)){for (; i < n && inOrder[i] != outOrder[out]; i++){rs[j++] = true;stk[st++] = inOrder[i];}//push ini++;//Watch out: don't forget while inOrder[i]==outOrder[out]!rs[j++] = true;rs[j++] = false;out++;while (st > 0 && stk[st-1] == outOrder[out]){st--; out++;rs[j++] = false;}//pop back}if (st == 0 && out == n){puts("Yes.");for (int i = 0; i < j; i++){if (rs[i]) puts("in");else puts("out");}}else puts("No.");puts("FINISH");}return 0;}




hdu1022 Train Problem I 總是不可以AC

這不是棧的應用嗎

你就把字串反一下是不對的, 火車的出入不一定按逆序輸出的,

火車 123 的形式進來, 132 出來
123 in 1 out 1 in 2 in 3 out 3 out 2 出來是 132, 你的代碼這樣就不對了

#include<iostream>
using namespace std;

int main()
{
int n,i,j,k;
char in[1000],out[1000];
int flag[2000];

while(cin>>n>>in>>out)
{
char stack[1000];
int top=-1;
k=0,i=0,j=0;
while(j<n+1&&i<n)
if(stack[top]==out[i]&&top!=-1)
{
top--;
flag[k]=0;
k++;i++;
}
else
{
top++;
flag[k]=1;k++;
stack[top]=in[j];j++;
}
if(k!=2*n){cout<<"No."<<endl<<"FINISH"<<endl;continue;}
else cout<<"Yes."<<endl;
for(i=0;i<n*2;i++)
if(flag[i])cout<<"in"<<endl;
else cout<<"out"<<endl;
cout<<"FINISH"<<endl;
}
}
 
hdu 1022 Train Problem I 為何總是WA???

#include <stdio.h>
#include <string.h>

char output[10000];

int stack[10000];
int num;
char in[10000];
char out[10000];

int main()
{
int n,inlen,outlen,ok,i,ptin,ptout;
while(scanf("%d",&n)==1)
{
num = 0;
scanf("%s%s",in,out);
inlen = strlen(in);
outlen = strlen(out);
strcpy(output,"Yes.\n");
if(inlen!=outlen)
{
printf("No.\nFINISH\n");
continue;
}
ok = 1;
ptin = ptout = 0;
while(ok&&ptout<outlen)
{
if(num==0 && ptin < inlen)
{
stack[num++] = in[ptin++];
strcat(output,"in\n");
}
else if(stack[num-1]==out[ptout])
{
num--;
strcat(output,"out\n");
ptout++;
}
else if(ptin<inlen)
{
stack[num++] = in[ptin++];
strcat(output,"in\n");
}
else
ok = 0;
}
if(ok)
printf("%s",output);
else
printf("No.\n");
printf("FINISH\n");
}
}
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.