hdoj 1361 Parencodings 2001 Asia Regional Teheran

来源:互联网 发布:鲁大师mac版 编辑:程序博客网 时间:2024/06/06 07:26

思路:模拟,根据序列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++;elself++;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;}


原创粉丝点击