栈——HDU1022

题目是栈的模拟进出。

先用数组来模拟一下栈的指针移动。

#include <stdio.h>
#include <stdlib.h>

int main(){
    //freopen("in.txt","r",stdin);
    int a[1000];
    char c[1000];
    char s1[1000],s2[1000];
    int n;
    //scanf("%d",&n);
    while(scanf("%d %s%s",&n,s1,s2)!=EOF){
        int m = 0,j = 0,k = 0;
        for(int i = 0; i < n;i++){
            a[j++] = 1;
            c[k++] = s1[i];
            while(c[k-1] == s2[m]&& k >= 0){
                a[j++] = -1;
                m++;
                k--;
            }
        }
        if(k <= 0){
            printf("Yes.
");
            for(int i = 0;i < j;i++){
                if(a[i] == 1)
                    printf("in
");
                else if(a[i] == -1) printf("out
");
            }
        }else
            printf("No.
");
        printf("FINISH
");
    }
    return 0;
}
原文地址:https://www.cnblogs.com/sean10/p/4976869.html