九度OJ 1366 栈的压入、弹出序列 【数据结构】
发布时间:2020-12-15 06:30:47 所属栏目:安全 来源:网络整理
导读:题目地址:http://ac.jobdu.com/problem.php?pid=1366 题目描述: 输入两个整数序列,第一个序列表示栈的压入顺序,请判断第二个序列是否为该栈的弹出顺序。假设压入栈的所有数字均不相等。例如序列1,2,3,4,5是某栈的压入顺序,序列4,5,1是该压栈序列对应的
题目地址:http://ac.jobdu.com/problem.php?pid=1366
#include <stdio.h> #define MAX 100000 int IsRight (int push[],int pop[],int n){ int i = 0; int j = 0; int top = 0; int stack[MAX]; stack[0] = push[0]; while (i < n ){ while (i < n && stack[top] != pop[j]){ ++i; ++top; stack[top] = push[i]; } while (stack[top] == pop[j]){ --top; ++j; } } while (top >= 0 && j < n){ if (stack[top] == pop[j]){ --top; ++j; } else return 0; } return 1; } int main(void){ int n; int push[MAX]; int pop[MAX]; int i; while (scanf ("%d",&n) != EOF){ for (i=0; i<n; ++i) scanf ("%d",&push[i]); for (i=0; i<n; ++i) scanf ("%d",&pop[i]); if (IsRight (push,pop,n)) printf ("Yesn"); else printf ("Non"); } return 0; } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |