POJ 1208

水模擬,順便學習了下deque

教訓是,一個很簡單的題都花了很久才A。非常需要耐心,磨性子,讀題理解。編碼過程也因爲模擬過程的煩躁,導致代碼一直疏漏很多關鍵細節。

If you really want. 寫代碼,是一件很需要靜下來和自己好好交流的事情

#include <iostream>
#include <algorithm>
#include <queue>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <string>
#include <stack>
#include <map>
#include <set>
#include <deque>
using namespace std;

const int maxn= 30;

string op, wy;
deque<int> S[maxn];
stack<int> md;
int pos[maxn];

void Clear(int x)
{
	int px= pos[x];
	while (x!= S[px].back()){
		S[S[px].back()].push_back(S[px].back());
		pos[S[px].back()]= S[px].back();
		S[px].pop_back();
	}
}
int main(int argc, char const *argv[])
{
	int n, a, b;
	scanf("%d", &n);
	for (int i= 0; i< n; ++i){
		pos[i]= i;
		S[i].push_back(i);
	}

	while (cin>>op && 'q'!= op[0]){
		cin>>a>>wy>>b;
		int pa= pos[a], pb= pos[b];
		if (pa== pb){
			continue;
		}
		if ('m'== op[0]){
			Clear(a);
		}
		if ('n'== wy[1]){
			Clear(b);
		}
		while (1){
			md.push(S[pa].back());
			if (S[pa].back()== a){
				S[pa].pop_back();
				break;
			}
			S[pa].pop_back();
		}
		while (!md.empty()){
			S[pb].push_back(md.top());
			pos[md.top()]= pb;
			md.pop();
		}
	}

	for (int i= 0; i< n; ++i){
		printf("%d:", i);
		while (!S[i].empty()){
			printf(" %d", S[i].front());
			S[i].pop_front();
		}
		putchar('\n');
	}
	return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章