您的位置:首页 > 产品设计 > UI/UE

1051.Pop Sequence

2013-03-11 10:51 295 查看
http://pat.zju.edu.cn/contests/pat-a-practise/1051

#include <stdio.h>
#include <string.h>
#include <stack>
using namespace std;

stack<int> ans;
int a[1005],b[1005];

int main()
{
int m,n,k,i;
while (scanf("%d%d%d",&m,&n,&k)!=EOF)        //m栈最大深度,n序列长度,k待检验序列个数
{
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
for (i=1;i<=n;i++)
{
a[i]=i;
}
while (k--)
{
while (!ans.empty())          //每次检测,清空栈
{
ans.pop();
}
bool flag=true;
for(i=1;i<=n;i++)
{
scanf("%d",&b[i]);
}
int *p=(a+1),*q=b+1;                   //注意数组从1~n保存值,所以指针后移一位
while (*q!=0)
{
if (ans.empty()&&*p!=0)                    //当前栈空,则进栈
{
ans.push(*p++);
}
while (ans.top()!=*q&&*p!=0)   //栈顶不等于出栈首元素,则继续进栈
{
ans.push(*p++);
if (ans.size()>m)      //超过栈的最大深度,false
{
flag=false;
}
}
if (ans.top()==*q)             //若相同,则出栈序列当前元素拿走,说明匹配
{
q++;
ans.pop();
continue;
}
if (*p==0&&ans.top()!=*q)      //若入栈序列到末尾与出栈首还不同,false
{
flag=false;
}
if (!flag)
{
break;
}
}
if (!flag)
{
printf("NO\n");
}
else
printf("YES\n");
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: