zoj 1259 Rails.

頭一次棧接觸這東西,糾結了我四五天。。

剛開始是沒理解題,沒注意reorganize的意思....重組啊!!!神馬是重組,就是亂七八糟的唄``

進的時候是1,2,3,4,5……,可以不在C中停留,一個一個進的話,到B中就是1,2,3,4,5……

要是幾組的話,情況就多了,C中會有積累。例如B是3,2,1,4,5.那麼就是123|4|5

學了不少,但主要的還是棧的思想

代碼層面嘛,那個0比較有意思。

就這些吧,上代碼

#include <stdio.h> #include <stdlib.h> #include <math.h> #include <string.h> #include <algorithm> using namespace std; int main() {   int stack[1005], A, top;    //棧,也就是C車站   int station_B[1005],B;        //進B的順序,也就是出棧的順序     int n, i;     int ok = 1;         start:;     scanf("%d", &n);     while( n )     {    memset(station_B,0,sizeof(station_B));    ok = 1;    scanf("%d", &station_B[1]);    if (station_B[1] == 0)         //當B[1]爲0,換車廂數n,goto到scanf n     {     printf("\n");     goto start;    }    else    {     for (i = 2; i <= n; i++)       {      scanf("%d", &station_B[i]);          }       A = B = 1;     top = 0; //注意top初值     while (B <= n)                      //進棧順序是1 2 3 4 5     {     if (station_B[B] == A)          //若B【】==A,說明進一個就出一個,棧裏沒有存      {       A++;       B++;      }      else if (top && station_B[B] == stack[top])  //棧的最頂元素在B[]中找到(位於最後一個單個進入的後面),若相等的話      {               //top向下,B向右,再判斷更早的元素stack[top-1]是否還等於b[B+1]       B++;       top--;      }       else if (A <= n)               //B【】!=A,說明棧裏有存數,並且是從小到大存,1,2,3...因爲進棧是那個順序      {       stack[++top] = A++;       //從小到大進棧      }      else      {       ok = 0;       break;      }     }    }    printf("%s\n", ok ? "Yes" : "No");         }   system("pause");   return 0;

}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章