POJ:1086-Parencodings
Parencodings
Time Limit: 1000MS Memory Limit: 10000K
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
题意就是给你一串括号,输入是第i个右括号左边有多少个左括号,要你输出第i个右括号左边有多少对匹配好的括号(包括自身)。
数据量这么小,肯定是模拟啊,直接用一个标记数组标记左括号的匹配情况,每次有一个右括号就从当前的右括号开始向左找,一直找到左边第一个没有被标记的左括号,并且记录到第一个没被标记的左括号为止有多少个标记的左括号就好。然后输出就行。
#include<stdio.h>
#include<cstring>
using namespace std;
const int maxn = 100;
int num[maxn];
bool l[maxn];
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(l,0,sizeof(l));
int n;
scanf("%d",&n);
for(int i=1; i<=n; i++)
scanf("%d",&num[i]);
for(int i=1; i<=n; i++)
{
int sum = 0;
int k = num[i];
while(k>0 && l[k])
{
k--;
sum++;
}
if(k != 0)//左边没有了左括号,右括号只能打光棍
{
l[k] = true;
printf("%d",sum+1);
}
else
printf("%d",sum);
if(i != n)
printf(" ");
}
printf("\n");
}
return 0;
}
POJ:1086-Parencodings的更多相关文章
- poj:4091:The Closest M Points
poj:4091:The Closest M Points 题目 描写叙述 每到饭点,就又到了一日几度的小L纠结去哪吃饭的时候了.由于有太多太多好吃的地方能够去吃,而小L又比較懒不想走太远,所以小L会 ...
- Poj OpenJudge 1068 Parencodings
1.Link: http://poj.org/problem?id=1068 http://bailian.openjudge.cn/practice/1068 2.Content: Parencod ...
- POJ:1182 食物链(带权并查集)
http://poj.org/problem?id=1182 Description 动物王国中有三类动物A,B,C,这三类动物的食物链构成了有趣的环形.A吃B, B吃C,C吃A. 现有N个动物,以1 ...
- 51Nod:1086背包问题 V2
1086 背包问题 V2 基准时间限制:1 秒 空间限制:131072 KB 分值: 40 难度:4级算法题 有N种物品,每种物品的数量为C1,C2......Cn.从中任选若干件放在容量为W的背包里 ...
- POJ:2229-Sumsets(完全背包的优化)
题目链接:http://poj.org/problem?id=2229 Sumsets Time Limit: 2000MS Memory Limit: 200000K Total Submissio ...
- POJ:3126-Prime Path
题目链接:http://poj.org/problem?id=3126 Prime Path Time Limit: 1000MS Memory Limit: 65536K Total Submiss ...
- POJ:2429-GCD & LCM Inverse(素数判断神题)(Millar-Rabin素性判断和Pollard-rho因子分解)
原题链接:http://poj.org/problem?id=2429 GCD & LCM Inverse Time Limit: 2000MS Memory Limit: 65536K To ...
- POJ :3614-Sunscreen
传送门:http://poj.org/problem?id=3614 Sunscreen Time Limit: 1000MS Memory Limit: 65536K Total Submissio ...
- POJ:2377-Bad Cowtractors
传送门:http://poj.org/problem?id=2377 Bad Cowtractors Time Limit: 1000MS Memory Limit: 65536K Total Sub ...
- POJ:2139-Six Degrees of Cowvin Bacon
传送门:http://poj.org/problem?id=2139 Six Degrees of Cowvin Bacon Time Limit: 1000MS Memory Limit: 6553 ...
随机推荐
- [干货分享]AXURE整套高保真UI框架和元件组(白色风格)
写在前面 强烈建议开始之前阅读以下第一篇高保真UI框架的前面部分,以了解设计思想,这篇文章不再重复介绍: AXURE-整套可复用的高保真元件和框架之暗黑风格 本次共享模板的UI规范 注:由于篇幅问 ...
- 前端开发如何做好SEO优化的工作
前端开发工程师不仅需要要跟视觉设计师.交互式设计师配合,完美还原设计图稿,编写兼容各大浏览器.加载速度快.用户体验好的页面.现在还需要跟SEO人员配合,调整页面的代码结构和标签. 一些成熟的平台,在开 ...
- JavaScript中登录名的正则表达式及解析(0基础)
简言 在JavaScript中,经常会用到正则表达式来进行模式匹配.例如,登录名验证,密码强度验证,字符串查找或替换等操作.现在就开始吧,零基础写出你的第一个正则表达式! 在做用户注册时,都会用到登录 ...
- springmvc学习经验
Spring MVC属于SpringFrameWork的后续产品,已经融合在Spring Web Flow里面.Spring 框架提供了构建 Web 应用程序的全功能 MVC 模块.使用 Spring ...
- Linux基础环境_安装配置教程(CentOS7.2 64、JDK1.8、Tomcat8)
Linux基础环境_安装配置教程 (CentOS7.2 64.JDK1.8.Tomcat8) 安装包版本 1) VMawre-workstation版本包 地址: https://my.vmw ...
- 洛谷 P1784 数独
题目描述 数独是根据9×9盘面上的已知数字,推理出所有剩余空格的数字,并满足每一行.每一列.每一个粗线宫内的数字均含1-9,不重复.每一道合格的数独谜题都有且仅有唯一答案,推理方法也以此为基础,任何无 ...
- 洛谷 P3119 [USACO15JAN]草鉴定Grass Cownoisseur
屠龙宝刀点击就送 Tarjan缩点+拓扑排序 以后缩点后建图看n范围用vector ,或者直接用map+vector 结构体里数据要清空 代码: #include <cstring> #i ...
- 洛谷 P2663 越越的组队
题目描述 班级要组织一场综合能力竞赛,全班同学(N个,N是偶数)分成两队互相竞争.老师找到了越越并给了越越一张全班同学综合能力测试的成绩,要求他从全班同学中选出一半(他自己也可能被选),并要求这些同学 ...
- codeforce Gym 100685F Flood (topo排序)
如果直接模拟水向周围流会TLE,因为某些个结点被重复扩展了多次, 科学做法是topo排序,每次只把入度为0的点放入队列,这样就严格保证了每个结点只被扩展一次. #include<bits/std ...
- BFS 简单思想以及代码
BFS(广搜思想) 广度优先搜索 广度优先搜索是图论的搜索算法之一,以下便进行简单叙述 对于每一个顶点来说,都存在着三种颜色 白色,灰色,黑色 而对于每个顶点,都有三种数据类型 颜色类型,前驱或者父节 ...