適配器

特殊的順序結構

stack (疊積木)

/*
 * AP325
 * P-3-2 括弧配對
*/
#include<bits/stdc++.h>
using namespace std;
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);

int f(char c){
	if(c=='('){
		return -1;
	}
	if(c==')'){
		return 1;
	}
	if(c=='['){
		return -2;
	}
	if(c==']'){
		return 2;
	}
	if(c=='{'){
		return -3;
	}
	if(c=='}'){
		return 3;
	}
}
int main(){
	string s;
	getline(cin,s);
	stack<int>x;
	for(int i=0;i<s.size();i++){
		if(!x.empty()){
			if(x.top()+f(s[i])==0){
				x.pop();
			}
		}else{
			x.push(f(s[i]));
		}
	}
	if(x.empty()){
		cout << "yes" << "\n";
	}else{
		cout << "no" << "\n";
	}
}

queue (排隊)

/*
 * example
*/
#include<bits/stdc++.h>
using namespace std;

int main() {
    queue<int> q;

    q.push(10);
    q.push(20);
    q.push(30);
    
    return 0;
}

priority_queue (能夠自定義的queue)

/*
 * example
*/
#include<bits/stdc++.h>
using namespace std;

struct Compare {
    bool operator()(int a, int b) {
        return a > b;
    }
};

int main() {
    priority_queue<int, vector<int>, Compare> Heap;
    
    Heap.push(30);
    Heap.push(10);
    Heap.push(50);
    Heap.push(40);
    
    return 0;
}

Last updated

Was this helpful?