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

UVA 11995 I Can Guess the Data Structure!

2014-12-24 19:21 197 查看
题意:给出若干组数据,每组数据包含t行,每行第一个数为1或2;1表示进入,2表示取出,根据出入顺序判断为何种数据结构

链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=18700

思路:需要判断的数据结构有三种,栈,队列,优先队列,即单纯的STL应用,判断每次取出是否与三种数据结构中的情况相等即可。

注意点:可能STL中为空的情况下继续取出,需要判断边界情况。

以下为AC代码:

RunID
User

OJ

UVA

Prob ID

Result

All
Memory

(KB)
Time

(ms)
Language

All

Length

(Bytes)
Submit Time
3110916luminous11
UVA
11995
Accepted
56C++11 4.8.2
195415 min ago
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <vector>
#include <deque>
#include <list>
#include <cctype>
#include <algorithm>
#include <climits>
#include <queue>
#include <stack>
#include <cmath>
#include <map>
#include <set>
#include <iomanip>
#include <cstdlib>
#include <ctime>
#define ll long long
#define ull unsigned long long
#define all(x) (x).begin(), (x).end()
#define clr(a, v) memset( a , v , sizeof(a) )
#define pb push_back
#define mp make_pair
#define read(f) freopen(f, "r", stdin)
#define write(f) freopen(f, "w", stdout)
using namespace std;

int main()
{
ios::sync_with_stdio( false );
queue<int> q;
stack<int> s;
priority_queue<int> pq;
int t;
while ( cin >> t )
{
int a, b;
bool f1 = 1;
bool f2 = 1;
bool f3 = 1;
while ( !  q.empty() )  q.pop();
while ( !  s.empty() )  s.pop();
while ( ! pq.empty() ) pq.pop();
for ( int i = 0; i < t; i ++ )
{
cin >> a >> b;
if ( a == 1 )
{
q.push( b );
s.push( b );
pq.push( b );
}
else if ( a == 2 && ! q.empty() )
{
if ( q.front() != b ) f1 = 0; q.pop();
if ( s.top() != b ) f2 = 0; s.pop();
if ( pq.top() != b ) f3 = 0; pq.pop();
}
else if ( a == 2 && q.empty() )
{
f1 = 0;
f2 = 0;
f3 = 0;
}
}
if      (     f1   && ( ! f2 ) && ( ! f3 ) ) cout << "queue" << endl;
else if (     f2   && ( ! f1 ) && ( ! f3 ) ) cout << "stack" << endl;
else if (     f3   && ( ! f1 ) && ( ! f2 ) ) cout << "priority queue" << endl;
else if ( ( ! f1 ) && ( ! f2 ) && ( ! f3 ) ) cout << "impossible" << endl;
else cout << "not sure" << endl;
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: