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('
');
	}
	return 0;
}
原文地址:https://www.cnblogs.com/Idi0t-N3/p/14722267.html