Description

Let S = s1 s2...s2n be a well-formed string of parentheses. S can be encoded in two different ways:
q By an integer sequence P = p1 p2...pn where pi is the number of left parentheses before the ith right parenthesis in S (P-sequence).
q By an integer sequence W = w1 w2...wn where for each right parenthesis, say a in S, we associate an integer which is the number of right parentheses counting from the matched left parenthesis of a up to a. (W-sequence).
Following is an example of the above encodings:


S (((()()())))
P-sequence 4 5 6666

W-sequence 1 1 1456

Write a program to convert P-sequence of a well-formed string to the W-sequence of the same string.

Input

The first line of the input contains a single integer t (1 <= t <= 10), the number of test cases, followed by the input data for each test case. The first line of each test case is an integer n (1 <= n <= 20), and the second line is the P-sequence of a well-formed string. It contains n positive integers, separated with blanks, representing the P-sequence.

Output

The output file consists of exactly t lines corresponding to test cases. For each test case, the output line should contain n integers describing the W-sequence of the string corresponding to its given P-sequence.

Sample Input

2
6
4 5 6 6 6 6
9
4 6 6 6 6 8 9 9 9

Sample Output

1 1 1 4 5 6
1 1 2 4 5 1 1 3 9

 
分析:
对于给出的序列,明显是一定不会下降的,
用ans[]来记录答案
当A[i]>A[j]时其对应的ans[i]=1;
注意到A[i]-ans[i]其实就是当前右括号对应的左括号的前面有多少个左括号
很明显对每个A[i]-ans[i]都是唯一的,用它来作为匹配过的左括号的编号,这个编号从0开始
所以当A[i]==A[i-1]时 令k=ans[i-1]+1;
不断增加k直到A[i]-k的值没有在前面出现过时,ans[i]=k;
 #include <iostream>
#include <cstring>
using namespace std;
int f[], g[], ans[];
int t, n, k, sum;
int main() {
cin >> t;
while (t--) {
cin >> n;
memset (g, , sizeof g);//数组g用来记录使用过的左括号
for (int i = ; i <= n; i++) {
cin >> f[i];
if (f[i] > f[i - ]) {//比前面的数大
ans[i] = ;//ans[i]赋值为一
g[f[i] - ans[i]] = ;//标记使用过的左括号
}
else {
int k = ans[i - ] + ;
while (g[f[i] - k]) k++;//找到没有使用过的左括号
g[f[i] - k] = ;
ans[i] = k;
}
}
cout << ans[];
for (int i = ; i <= n; i++)
cout << ' ' << ans[i];
cout << endl;
}
return ;
}
 
http://www.cnblogs.com/keam37/ keam所有 转载请注明出处

POJ1068 Parencodings 解题报告的更多相关文章

  1. hdu 1361.Parencodings 解题报告

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1361 题目意思: 根据输入的P-sequence , 输出对应的W-sequence.   P-se ...

  2. 北大ACM试题分类+部分解题报告链接

    转载请注明出处:優YoU http://blog.csdn.net/lyy289065406/article/details/6642573 部分解题报告添加新内容,除了原有的"大致题意&q ...

  3. CH Round #56 - 国庆节欢乐赛解题报告

    最近CH上的比赛很多,在此会全部写出解题报告,与大家交流一下解题方法与技巧. T1 魔幻森林 描述 Cortana来到了一片魔幻森林,这片森林可以被视作一个N*M的矩阵,矩阵中的每个位置上都长着一棵树 ...

  4. 二模13day1解题报告

    二模13day1解题报告 T1.发射站(station) N个发射站,每个发射站有高度hi,发射信号强度vi,每个发射站的信号只会被左和右第一个比他高的收到.现在求收到信号最强的发射站. 我用了时间复 ...

  5. BZOJ 1051 最受欢迎的牛 解题报告

    题目直接摆在这里! 1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec  Memory Limit: 162 MBSubmit: 4438  Solved: 2353[S ...

  6. 习题:codevs 2822 爱在心中 解题报告

    这次的解题报告是有关tarjan算法的一道思维量比较大的题目(真的是原创文章,希望管理员不要再把文章移出首页). 这道题蒟蒻以前做过,但是今天由于要复习tarjan算法,于是就看到codevs分类强联 ...

  7. 习题:codevs 1035 火车停留解题报告

    本蒟蒻又来写解题报告了.这次的题目是codevs 1035 火车停留. 题目大意就是给m个火车的到达时间.停留时间和车载货物的价值,车站有n个车道,而火车停留一次车站就会从车载货物价值中获得1%的利润 ...

  8. 习题: codevs 2492 上帝造题的七分钟2 解题报告

    这道题是受到大犇MagHSK的启发我才得以想出来的,蒟蒻觉得自己的代码跟MagHSK大犇的代码完全比不上,所以这里蒟蒻就套用了MagHSK大犇的代码(大家可以关注下我的博客,友情链接就是大犇MagHS ...

  9. 习题:codevs 1519 过路费 解题报告

    今天拿了这道题目练练手,感觉自己代码能力又增强了不少: 我的思路跟别人可能不一样. 首先我们很容易就能看出,我们需要的边就是最小生成树算法kruskal算法求出来的边,其余的边都可以删掉,于是就有了这 ...

随机推荐

  1. JDK集合框架--LinkedList

    上一篇讲了ArrayList,它有一个"孪生兄弟"--LinkedList,这两个集合类总是经常会被拿来比较,今天就分析一下LinkedList,然后总结一下这俩集合类的不同 首先 ...

  2. 契约式设计(DbC)感想(二)

    契约式设计6大原则的理解 在<Design by Contract原则与实践>中,作者定义了契约式设计的6大原则: 区分命令和查询: 将基本查询和派生查询区分开: 针对每个派生查询,设定一 ...

  3. 什么是LambdaExpression,如何转换成Func或Action(2)

    序言 在上一篇中,我们认识了什么是表达式树.什么是委托,以及它们的关系.可能是我功力不好,貌似大家都不怎么关注,没有讲解出不同角度的问题. 学习一种新技术,是枯燥的过程,只有在你掌握后并能运用时才能从 ...

  4. vue2.0 路由知识一(路由的创建的全过程)

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  5. 富文本KindEditor使用

    1.官网down KindEditor,添加到自己的项目中:添加时可把不需要的文件夹干掉,asp/php等等.我的项目用的是纯html和js,直接调用后台api: 2.页面引入相关js.eclipse ...

  6. 【转帖】迅为iTOP-iMX6开发板 Ubuntu系统下WiFi模块mt6620的移植

    本文转自迅为论坛 :http://www.topeetboard.com 文档提供的文件如下. wpa_supplicant 拷贝到开发板 Ubuntu 系统的 /sbin 目录下,如何移植 wpa_ ...

  7. CAD参数绘制多段线(网页版)

    多段线又被称为多义线,表示一起画的都是连在一起的一个复合对象,可以是直线也可以是圆弧并且它们还可以加不同的宽度. 主要用到函数说明: _DMxDrawX::PathLineTo 把路径下一个点移到指定 ...

  8. echo追加字符串到文件末尾

    1.覆盖    echo  "string" >  filename 2.追加   echo "string"  >>  filename

  9. 1.C#冒泡排序

    冒泡 比较相邻的元素.如果第一个比第二个大,就交换他们两个. 对每一对相邻元素作同样的工作,从开始第一对到结尾的最后一对.这步做完后,最后的元素会是最大的数. 针对所有的元素重复以上的步骤,除了最后一 ...

  10. Spring Cloud和Dubbo的对比