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. AJPFX关于static总结

    static 总结 static Fields        static Methods        static member class        static initializer-- ...

  2. 工具类学习-java实现邮件发送激活码

    问题:用java实现服务器发送激活码到用户邮件. 步骤一:如果是个人的话,确保在本地安装邮件服务器(易邮服务器)和邮件客户端(foxmail). 步骤二:导入jar包  mail.jar,其他的需要什 ...

  3. Sql中创建事务处理

    Create Procedure MyProcedure AS Begin Set NOCOUNT ON; Set XACT_ABORT ON; --这句话非常重要 begin try Begin T ...

  4. 关于Android软键盘把布局顶上去的问题

    首先说下我的需求:布局最上面是一个bar,有左上角返回按钮和标题,bar下面是一个ScrollView,里面有各种TextView和EditText, 点击下面的EditText时,不希望软键盘把ba ...

  5. jboss中JVM监控

    1)打开 http://server-name-or-ip/jmx-console/HtmlAdaptor2)在 jboss.system 节点找到 type=ServerInfo ,点击进入3)找到 ...

  6. 【转】Nutz | Nutz项目整合Spring实战

    http://blog.csdn.net/evan_leung/article/details/54767143 Nutz项目整合Spring实战 前言 Github地址 背景 实现步骤 加入spri ...

  7. bind - 将一个名字和一个套接字绑定到一起

    SYNOPSIS 概述 #include <sys/types.h> #include <sys/socket.h> int bind(int sockfd, struct s ...

  8. java将很长的一条sql语句,自动换行输出(修改版)2019-06-01(bug未修复)

    package org.jimmy.autosearch2019.test; import java.util.HashMap; public class AutoLinefeedSql { publ ...

  9. Sql Server cross apply和outer apply

    with result as( select t.str from( ' str union all ' str union all ' str union all ' str union all ' ...

  10. vue 清除keep-Alive页面缓存