POJ1068 Parencodings 解题报告
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 1456Write 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 解题报告的更多相关文章
- hdu 1361.Parencodings 解题报告
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1361 题目意思: 根据输入的P-sequence , 输出对应的W-sequence. P-se ...
- 北大ACM试题分类+部分解题报告链接
转载请注明出处:優YoU http://blog.csdn.net/lyy289065406/article/details/6642573 部分解题报告添加新内容,除了原有的"大致题意&q ...
- CH Round #56 - 国庆节欢乐赛解题报告
最近CH上的比赛很多,在此会全部写出解题报告,与大家交流一下解题方法与技巧. T1 魔幻森林 描述 Cortana来到了一片魔幻森林,这片森林可以被视作一个N*M的矩阵,矩阵中的每个位置上都长着一棵树 ...
- 二模13day1解题报告
二模13day1解题报告 T1.发射站(station) N个发射站,每个发射站有高度hi,发射信号强度vi,每个发射站的信号只会被左和右第一个比他高的收到.现在求收到信号最强的发射站. 我用了时间复 ...
- BZOJ 1051 最受欢迎的牛 解题报告
题目直接摆在这里! 1051: [HAOI2006]受欢迎的牛 Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 4438 Solved: 2353[S ...
- 习题:codevs 2822 爱在心中 解题报告
这次的解题报告是有关tarjan算法的一道思维量比较大的题目(真的是原创文章,希望管理员不要再把文章移出首页). 这道题蒟蒻以前做过,但是今天由于要复习tarjan算法,于是就看到codevs分类强联 ...
- 习题:codevs 1035 火车停留解题报告
本蒟蒻又来写解题报告了.这次的题目是codevs 1035 火车停留. 题目大意就是给m个火车的到达时间.停留时间和车载货物的价值,车站有n个车道,而火车停留一次车站就会从车载货物价值中获得1%的利润 ...
- 习题: codevs 2492 上帝造题的七分钟2 解题报告
这道题是受到大犇MagHSK的启发我才得以想出来的,蒟蒻觉得自己的代码跟MagHSK大犇的代码完全比不上,所以这里蒟蒻就套用了MagHSK大犇的代码(大家可以关注下我的博客,友情链接就是大犇MagHS ...
- 习题:codevs 1519 过路费 解题报告
今天拿了这道题目练练手,感觉自己代码能力又增强了不少: 我的思路跟别人可能不一样. 首先我们很容易就能看出,我们需要的边就是最小生成树算法kruskal算法求出来的边,其余的边都可以删掉,于是就有了这 ...
随机推荐
- linux下安装xampp
Choose your flavor for your linux OS, the 32-bit or 64-bit version. Change the permissions to the in ...
- windows 安装绿色版mysql
(1)到官网下载绿色版mysql:http://dev.mysql.com/downloads/mysql/ (2)下载好后,放在F:\mysql,解压出来 (3)进入到mysql-5.6.19-wi ...
- ASP.Net TextBox只读时不能通过后台赋值取值
给页面的TextBox设置ReadOnly="True"时,在后台代码中不能赋值取值,下边几种方法可以避免: 1.不设置ReadOnly,设置onfocus=this.blur() ...
- BotFramework学习-01
微软在Build2016大会上表示,未来将是一个充满聊天机器人的世界,为此他们推出了微软Bot Framework,能够允许任何人制作自己的聊天机器人,微软则提供“cognitive microser ...
- windows ubuntu bcdeditor
双系统. 先装windows,后装ubuntu12.04 为了避免grub引导,所以安装bcdeditor. 平时使用没有什么不适,可是每次linux升级内核以后,grub列表可能就会消失,自然是不能 ...
- Ryubook_1_switch_hub_部署执行
一.环境: mininet.ovs.Ryu. 二.实验过程: 1.搭建拓扑: 执行sudo mn --topo single,3 --mac --switch ovsk --controller re ...
- Which dispatch method would be used in Swift?
In this example: protocol MyProtocol { func testFuncA() } extension MyProtocol { func testFuncA() { ...
- CV方向:纽劢科技,复星,蚂蚁金服,哈喽出行,海康威视
以下均为CV方向实习面试,岗位算法工程师助理.时间均在2019年3月至4月之间. 纽劢科技(均电话): 技术2面,HR1面 复星(现场): 技术只有1面 ...
- TensorFlow低阶API(一)—— 简介
简介 本文旨在知道您使用低级别TensorFlow API(TensorFlow Core)开始编程.您可以学习执行以下操作: 管理自己的TensorFlow程序(tf.Graph)和TensorFl ...
- HTTP的缺点与HTTPS
a.通信使用明文不加密,内容可能被窃听 b.不验证通信方身份,可能遭到伪装 c.无法验证报文完整性,可能被篡改 HTTPS就是HTTP加上加密处理(一般是SSL安全通信线路)+认证+完整性保护