zoukankan      html  css  js  c++  java

Basic Data Structure

Time Limit: 7000/3500 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 982    Accepted Submission(s): 253

Problem Description
Mr. Frog learned a basic data structure recently, which is called stack.There are some basic operations of stack：

Input
The first line contains only one integer T (

Output
For each test case, first output one line "Case #x：w, where x is the case number (starting from 1). Then several lines follow,  i-th line contains an integer indicating the answer to the i-th QUERY operation. Specially, if the i-th QUERY is invalid, just print "Invalid."(without quotes). (Please see the sample for more details.)

Sample Input
2 8 PUSH 1 QUERY PUSH 0 REVERSE QUERY POP POP QUERY 3 PUSH 0 REVERSE QUERY

Sample Output
Case #1: 1 1 Invalid. Case #2: 0
Hint
In the first sample： during the first query, the stack contains only one element 1, so the answer is 1. then in the second query, the stack contains 0, l (from bottom to top), so the answer to the second is also 1. In the third query, there is no element in the stack, so you should output Invalid.

Source

Recommend
wange2014   |   We have carefully selected several similar problems for you:  5932 5931 5930 5928 5927
/*
双向队列，记录从开头开始到第一个0的位置的1有多少个，因为0与任何nand都是1

比赛的时候竟然想不起来双向队列.......愣是用一个数组加了两个指针模拟了一个双向队列。
*/
#include<bits/stdc++.h>
#define N 500000
using namespace std;
int s[N];
deque<int >q;//用来存放所有0的位置
int main()
{
//freopen("C:\Users\acer\Desktop\in.txt","r",stdin);
int t,n;
char op[20];
scanf("%d",&t);
int Case=1;
while(t--)
{
memset(s,-1,sizeof s);
int f=1;
scanf("%d",&n);
int r=250001;
int l=r-1;
int fa=0;///记录栈里面的总数
q.clear();
printf("Case #%d:
",Case++);
while(n--)
{
scanf("%s",op);
int a;
if(op[0]=='P'&&op[1]=='U')
{
scanf("%d",&a);
if(f)
{
s[r]=a;
if(!a)
q.push_back(r);
r++;
}
else
{
s[l]=a;
if(!a)
q.push_front(l);
l--;
}
fa++;
}
else if(op[0]=='P'&&op[1]=='O')
{
if(!fa)
continue;
if(f)
{
if(s[r-1]==0)
q.pop_back();
r--;
}
else
{
if(s[l+1]==0)
q.pop_front();
l++;
}
fa--;
}
else if(op[0]=='Q')
{
//cout<<"cur="<<cur<<endl;
int cur=0;
if(fa==0)
{
cout<<"Invalid."<<endl;
}
else if(fa==1)
{
cout<<s[l+1]<<endl;
}
else
{
if(f)
{
if(q.empty())
cur=fa;
else
{
cur=q.front()==r-1?fa-1:q.front()-l;
}
}
else
{
if(q.empty())
cur=fa;
else
{
cur=q.back()==l+1?fa-1:r-q.back();
}

}
if(cur%2==0)
cout<<"0"<<endl;
else
cout<<"1"<<endl;
}
}
else
{
f^=1;
}
}
}
return 0;
}
• 相关阅读:
S1-概论
AngularJS--day01介绍使用基本语法
原生ajax--2
原生ajax---1
操作元素--字符串对象-日期对象-Array对象（数组）-Math对象-计时器
HTTP协议系列教材 （三）- 通过Firefox火狐调试工具观察 HTTP 请求协议
HTTP协议系列教材 （二）- 借助FireFox火狐调试工具学习HTTP协议
HTTP协议系列教材 （一）- 教程
Servlet系列教材 （二十七）- JSON
Servlet系列教材 （二十六）- JSON
• 原文地址：https://www.cnblogs.com/wuwangchuxin0924/p/6005096.html