hdu 1022 Train Problem I 棧模擬

難度:2

算法:模擬一個棧

大家只要瞭解一下棧的實現就能很好的模擬出這道提

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
int n , sta[100] , od[100] , cnt;
char c1[100] , c2[100];
bool check() {
    cnt = 0;
    int i = 0 , j = 0 , s = 0;
    while(j < n) {
        if(c1[i] == c2[j]) {
            od[cnt++] = 1;
            od[cnt++] = 0;
            i++; j++;
        }
        else if(s > 0 && sta[s-1] == c2[j]) {
            od[cnt++] = 0;
            s--;
            j++;
        }
        else if(i < n) {
            od[cnt++] = 1;
            sta[s++] = c1[i];
            i++;
        }
        else return false;
    }
    return true;
}
int main() {
    while(~scanf("%d%s%s" , &n , c1 , c2)) {
        if(!check()) {
            puts("No.");
            puts("FINISH");
        }
        else {
            puts("Yes.");
            for(int i=0;i<cnt;i++) {
                if(od[i] == 1) puts("in");
                else puts("out");
            }
            puts("FINISH");
        }
    }
    return 0;
}


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