5、STL之栈、队列与优先队列

5.1 栈

集合栈计算器-UVA12096

大意:

在栈中执行四种不同的操作

知识点:

typedef set Set:开空集合直接Set()就可以了!
define:

define的多种用法(常数定义、带参定义、代码段定义、条件宏定义以及和template的区别)

#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin()) // 记住就行,用于set的union、intersection等操作

代码:

#include<iostream>
#include<vector>
#include<set>
#include<stack>
#include<map>
#include<algorithm>
#include<iterator>
using namespace std;
typedef set<int> Set;  //开空集合直接Set()就可以了!
map<Set,int> IDmap;  // Set和int的映射
vector<Set> IDvec;  // 用下标来访问Set元素
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())  // 记住就行,用于set的操作
int setID_get(Set x){
 if(IDmap.count(x)){
  return IDmap[x];
 }
 else{
  IDvec.push_back(x);
  IDmap[x] =  IDvec.size()-1;
  return IDmap[x];
 }
}
int main(){
 stack<int> s;
 int kase;
 cin >> kase;
 while(kase--){
  int n;
  cin >> n;
  while(n--){
   string op;
   cin >> op;
   if (op[0]=='P'){
    s.push(setID_get(Set()));
   }
   else if (op[0]=='D'){
    s.push(s.top());
   }
   else{
    Set x1 = IDvec[s.top()]; s.pop();
    Set x2 = IDvec[s.top()]; s.pop();
    Set ans;
    if(op[0]=='U') set_union(ALL(x1),ALL(x2),INS(ans));
    if(op[0]=='I') set_intersection(ALL(x1),ALL(x2),INS(ans));
    if(op[0]=='A') ans = x2; ans.insert(setID_get(x1));
    s.push(setID_get(ans));
   }
   cout << IDvec[s.top()].size() << endl;
  }
  cout << "***" << endl;
 }
 return 0;

ps:上面的代码有一些错误但是时间关系不去管它了

5.2队列

例题:

Team Queue - UVA540

大意:

定义两个队列,其中一个是总队列,另外一个通过下标访问得到每一个队伍的队列。插入某队员时,如果总队列里有自己的队友,则可以直接插到他后面(迷惑描述,实际解题顺着他想,其实没那么复杂)。如果没有自己的队友,就直接插到总队列的最后。其实用了两个队列就不麻烦了。

知识点:

队列的使用:

队列的使用转自 眼里有星河

#include<queue>
queue<int> ***;
***.front() // 访问第一个
empty();
push();
pop();
等等等等

代码:

#include<iostream>
#include<map>
#include<queue>
#include<string>
using namespace std;
const int maxn = 1000 + 10;
map<int,int> teammap;
int main(){
 int cnt = 1;
 while(1){
  int n;  // 有n队
  cin >> n;
  if(n==0){
   break;
  }
  for(int i = 0;i<n;i++){
   int teamnum;  // 有teamnum个队员
   cin >> teamnum;
   while(teamnum--){
    int teammate;
    cin >> teammate;
    teammap[teammate] = i;
   }
  }
  cout << "Scenario #" << cnt << endl;
  cnt++;
  queue<int> qall,qteam[maxn];
  for(;;){
   string cmd;  // char cmd[10]
   int x;
   cin >> cmd;
   if(cmd[0] == 'S'){
    break;
   }
   else if(cmd[0] == 'E'){
    cin >> x;
    int t = teammap[x];
    if(qteam[t].empty()){
     qall.push(t);
    }
    qteam[t].push(x);
   }
   else if(cmd[0]=='D'){
    int t = qall.front();
    cout << qteam[t].front() << endl; qteam[t].pop();
    if(qteam[t].empty()){
     qall.pop();
                }
   }
  }
  cout << endl;
 }
}

5.3优先队列

解释:

优先队列

结构体重载运算符:

结构体中重载运算符转自 sunny1996

例题:

丑数-UVA 136

大意:

不能被2,3,5以外其他素数整除的数
输出第1500个丑数

知识点:

优先队列与集合的配合

代码:

#include<iostream>
#include<queue>
#include<set>
using namespace std;
const int basenum[3] = {2,3,5};
typedef long long LL;
int main(){
    priority_queue<LL,vector<LL>,greater<LL> >pg;
    set<LL> s;
    pg.push(1);
 s.insert(1);
 for(int i = 1;;i++){
  LL x = pg.top();pg.pop();
  if(i==1500){
   cout << "The 1500'th ugly number is <" << x << ">." << endl;
   break;
  }
  for(int i = 0;i<3;i++){
   LL newx = x*basenum[i];
   if(s.count(newx)==0){
    s.insert(newx);
    pg.push(newx);
   }
  }
 }
 return 0;
}
发布了45 篇原创文章 · 获赞 0 · 访问量 1007

猜你喜欢

转载自blog.csdn.net/jokerxsy/article/details/104130481
今日推荐