hdoj 1361 Parencodings 2001 Asia Regional Teheran

思路:模擬,根據序列P可以構造出S,然後再根據S,推出序列W。

#include <iostream>
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>

using namespace std;

char str[50];
int w[25];

int main()
{
	int t, n, num;
	scanf("%d", &t);
	while (t--) {
		scanf("%d", &n);
		int i ,j = 0, cnt = 0;
		for (i = 0; i < n; ++i) {
			scanf("%d", &num);
			for (; j < num; ++j)
				str[cnt++] = '(';
			str[cnt++] = ')';
		}
		n *= 2;
		int lf, rt;
		cnt = 0;
		for(i = 0; i < n; ++i) {
			if (str[i] == '(')
				continue;
			lf = 0;
			rt = 1;
			j = i - 1;
			while (1) {
				if (str[j] == ')')
					rt++;
				else
					lf++;
				if(rt == lf) {
				    w[cnt++] = rt;
					break;
				}
				j--;
			}
		}
		for (i = 0; i < cnt-1; ++i)
			printf("%d ", w[i]);
		printf("%d\n", w[cnt-1]);
	}
    return 0;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章