poj 1068 Parencodings(模拟)

题目链接:POJ 1068-Parencodings


题目大意:对与一个全为括号的序列,定义两种表示方法,P-sequence,表示第i个右括号左边有几个左括号。W-sequence表示第i个右括号与它左边的第j个左括号配对,现在给出P-sequence,求W-sequence。


解题思路:简单的模拟,因为总数不会大于20,所以不用考虑的太复杂。


#include <stdio.h>
#include <string.h>
const int N = 50;

int n, f[N], w[N], s[N];

int search(int x) {
	for (int i = x - 1, cnt = 1; i >= 0; i--) {
		if (s[i]) {
			if (s[i] == 1) {
				s[i] = 2;
				return cnt;
			}
			cnt++;
		}
	}
	return -1;
}

void solve() {
	int rec = 0, p = 0;
	memset(s, 0, sizeof(s));
	for (int i = 0; i < n; i++) {
		// draw;
		int cnt = f[i] - rec;
		rec = f[i];
		for (int j = 0; j < cnt; j++)
			s[p++] = 1;

		w[i] = search(p++);
	}
}

int main () {
	int cas;
	scanf("%d", &cas);
	while (cas--) {
		// read;
		scanf("%d", &n);
		for (int i = 0; i < n; i++)
			scanf("%d", &f[i]);

		// solve;
		solve();

		// printf;
		printf("%d", w[0]);
		for (int i = 1; i < n; i++)
			printf(" %d", w[i]);
		printf("\n");
	}
}



你可能感兴趣的:(poj 1068 Parencodings(模拟))