I - A Stack or A Queue?ZOJ - 3210
Input
Output
Sample Input
4
3
1 2 3
3 2 1
3
1 2 3
1 2 3
3
1 2 1
1 2 1
3
1 2 3
2 3 1
Sample Output
stack
queue
both
neither
AC代码:
#include<iostream>
#include<cstdio>
#include<string.h>
#include<cstring>
#include<queue>
#include<vector>
#include<set>
#include<stack>
#include<map>
#include<climits>
using namespace std;
const int N=110;
#define LL long long
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
int n;
scanf("%d",&n);
int a[N];
for(int i=0;i<n;i++)
cin>>a[i];
int flag1=1,flag2=1;
for(int i=0;i<n;i++)
{
int x;
cin>>x;
if(x!=a[i])
flag1=0;
if(x!=a[n-1-i])
flag2=0;
}
if(flag1&&flag2)
cout<<"both"<<endl;
else if(flag1)
cout<<"queue"<<endl;
else if(flag2)
cout<<"stack"<<endl;
else
cout<<"neither"<<endl;
}
return 0;
}
4 3 1 2 3 3 2 1 3 1 2 3 1 2 3 3 1 2 1 1 2 1 3 1 2 3 2 3 1
stack queue both neither
AC代码:
#include<iostream> #include<cstdio> #include<string.h> #include<cstring> #include<queue> #include<vector> #include<set> #include<stack> #include<map> #include<climits> using namespace std; const int N=110; #define LL long long int main() { int t; scanf("%d",&t); while(t--) { int n; scanf("%d",&n); int a[N]; for(int i=0;i<n;i++) cin>>a[i]; int flag1=1,flag2=1; for(int i=0;i<n;i++) { int x; cin>>x; if(x!=a[i]) flag1=0; if(x!=a[n-1-i]) flag2=0; } if(flag1&&flag2) cout<<"both"<<endl; else if(flag1) cout<<"queue"<<endl; else if(flag2) cout<<"stack"<<endl; else cout<<"neither"<<endl; } return 0; }