uva 11995 I Can Guess the Data Structure! (STL의 간단한 응용)
3920 단어 STL
I Can Guess the Data Structure!
There is a bag-like data structure, supporting two operations:
1 x
Throw an element x into the bag.
2
Take out an element from the bag.
Given a sequence of operations with return values, you're going to guess the data structure. It is a stack (Last-In, First-Out), a queue (First-In, First-Out), a priority-queue (Always take out larger elements first) or something else that you can hardly imagine!
Input
There are several test cases. Each test case begins with a line containing a single integer n (1<=n<=1000). Each of the next n lines is either a type-1 command, or an integer 2 followed by an integer x. That means after executing a type-2 command, we get an element x without error. The value of x is always a positive integer not larger than 100. The input is terminated by end-of-file (EOF). The size of input file does not exceed 1MB.
Output
For each test case, output one of the following:
stack
It's definitely a stack.
queue
It's definitely a queue.
priority queue
It's definitely a priority queue.
impossible
It can't be a stack, a queue or a priority queue.
not sure
It can be more than one of the three data structures mentioned above.
Sample Input
6
1 1
1 2
1 3
2 1
2 2
2 3
6
1 1
1 2
1 3
2 3
2 2
2 1
2
1 1
2 2
4
1 2
1 1
2 1
2 2
7
1 2
1 5
1 1
1 3
2 5
1 4
2 4
Output for the Sample Input
queue
not sure
impossible
stack
priority queue
제목: 두 가지 작업을 지원하는 "패키지"와 유사한 데이터 구조가 있습니다.
1a. 요소 a를 가방에 넣는다.
2a가방에서 a를 꺼낸다
이 가방이 무엇을 대표하는지 일련의 조작을 보여 주십시오.창고일 수도 있고, 대기열일 수도 있고, 우선 대기열 (수치가 큰 선출) 이나 다른 것일 수도 있다.
분석: 문제는 어렵지 않다. 한 가지 문제를 주의해야 한다.'가방'이 비어 있을 때 가방에서 물건을 찾을 수 없다.
코드:
#include
#include
#include
using namespace std;
int main()
{
int n, i, k, a, f[4];
while(~scanf("%d",&n))
{
stack s;
queue q;
priority_queue, less > pq;
for(i = 0; i < 3; i++) f[i] = 1;
for(i = 0; i < n; i++)
{
scanf("%d%d",&k,&a);
if(k == 1)
{
s.push(a);
q.push(a);
pq.push(a);
}
else
{
if(!s.empty())
{
if(s.top() != a)
f[0] = 0;
s.pop();
}
else
f[0] = 0;
if(!q.empty())
{
if(q.front() != a)
f[1] = 0;
q.pop();
}
else
f[1] = 0;
if(!pq.empty())
{
if(pq.top() != a)
f[2] = 0;
pq.pop();
}
else
f[2] = 0;
}
}
int cnt = 0;
for(i = 0; i < 3; i++)
if(f[i] == 1)
cnt++;
if(cnt == 0)
printf("impossible
");
else if(cnt > 1)
printf("not sure
");
else
{
if(f[0] == 1)
printf("stack
");
else if(f[1] == 1)
printf("queue
");
else
printf("priority queue
");
}
}
return 0;
}
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
시각화(3D) > 정사각형 원추(격자 모양의 디폴트로 구성) > GitHub+STL 렌더링의 디스플레이 >make_cubeGroup_180520.py v0.1작업 환경 연관 Ref1. Ref2. Ref3. GitHub STL 렌더링 환경에 관계없이 Ref1 구현 결과를 렌더링합니다. Unity 렌더링을 사용하여 최대 52만 개를 시도했습니다. 그러나 유니티가 없는 환경에...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.