Poj OpenJudge 1068 Parencodings
1.Link:
http://poj.org/problem?id=1068
http://bailian.openjudge.cn/practice/1068
2.Content:
Parencodings
Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20077 Accepted: 12122 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 9Sample Output
1 1 1 4 5 6
1 1 2 4 5 1 1 3 9Source
3.Method:
(1)先将P-sequence转成S序列,方法为:利用vector保存,置入)前,放入当前数值减前一数值的(
(2)将S序列转为W序列,方法为:利用stack,每次遇到(则置入stack,其中-1代表“(”;遇到count = 1,直到遇到第一个(前,将stack的值累加到count,并置出
4.Code:
#include <iostream>
#include <vector>
#include <stack> using namespace std; int main()
{
//freopen("D://input.txt","r",stdin); int i,j; int t;
cin >> t;
while(t--)
{
int n;
cin >> n; int *arr_p = new int[n]; for(i = ; i < n; ++i) cin >> arr_p[i]; //for(i = 0; i < n; ++i) cout << arr_p[i] << endl; vector<char> v_sym; int pre_p = ;
for(i = ; i < n; ++i)
{
for(j = pre_p; j < arr_p[i]; ++j) v_sym.push_back('(');
v_sym.push_back(')');
pre_p = arr_p[i];
} vector<char>::size_type sym_i; //for(sym_i = 0; sym_i != v_sym.size(); ++sym_i) cout << v_sym[sym_i];
//cout << endl; stack<int> s_sym;// -1 (, -2 )
for(sym_i = ; sym_i != v_sym.size(); ++sym_i)
{
if('(' == v_sym[sym_i]) s_sym.push(-);
else
{
int count = ;
while(s_sym.top() != -)
{
count += s_sym.top();
s_sym.pop();
}
s_sym.pop();
cout << count << " ";
s_sym.push(count);
}
}
cout << endl; delete [] arr_p; } return ;
}
5.Reference:
Poj OpenJudge 1068 Parencodings的更多相关文章
- 模拟 POJ 1068 Parencodings
题目地址:http://poj.org/problem?id=1068 /* 题意:给出每个右括号前的左括号总数(P序列),输出每对括号里的(包括自身)右括号总数(W序列) 模拟题:无算法,s数组把左 ...
- POJ 1068 Parencodings【水模拟--数括号】
链接: http://poj.org/problem?id=1068 http://acm.hust.edu.cn/vjudge/contest/view.action?cid=27454#probl ...
- POJ 1068 Parencodings 模拟 难度:0
http://poj.org/problem?id=1068 #include<cstdio> #include <cstring> using namespace std; ...
- poj 1068 Parencodings(栈)
题目链接:http://poj.org/problem?id=1068 思路分析:对栈的模拟,将栈中元素视为广义表,如 (((()()()))),可以看做 LS =< a1, a2..., a1 ...
- POJ 1068 Parencodings (类似括号的处理问题)
Pare ...
- poj 1068 Parencodings(模拟)
转载请注明出处:viewmode=contents">http://blog.csdn.net/u012860063?viewmode=contents 题目链接:http://poj ...
- POJ 1068 Parencodings
Parencodings Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24932 Accepted: 14695 De ...
- [ACM] POJ 1068 Parencodings(模拟)
Parencodings Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 19352 Accepted: 11675 De ...
- poj 1068 Parencodings 模拟
进入每个' ) '多少前' ( ', 我们力求在每' ) '多少前' ) ', 我的方法是最原始的图还原出来,去寻找')'. 用. . #include<stdio.h> #incl ...
随机推荐
- HDU 3410 && POJ 3776 Passing the Message 单调队列
题意: 给定n长的数组(下标从1-n)(n个人的身高,身高各不同样 问:对于第i个人,他能看到的左边最矮的人下标.(假设这个最矮的人被挡住了,则这个值为0) 还有右边最高的人下标,同理若被挡住了则这个 ...
- Ununtu 12.04 gedit安装插件Source Code Browser
1. 安装ctags: sudo apt-get install exuberant-ctags 2. 打开https://github.com/Quixotix/gedit-source-code- ...
- Laravel 5.1中 Redis 的安装配置及基本使用教程
关于Redis的介绍我们在之前Laravel 缓存配置一节中已有提及,Redis是一个开源的.基于内存的数据结构存储器,可以被用作数据库.缓存和消息代理.相较Memcached而言,支持更加丰富的数据 ...
- 双系统下,Windows如何正确删除Linux系统
一般电脑装了双系统,特别是Windows加Linux的电脑,不可以在Windows中直接删了linux,因为一般安装linux的时候,grub都写进了mbr,直接删了Windows就进不了了,除非原来 ...
- css预处理的引入与问题
css的预处理越来越流行.sass,less,stylus这几个都使用方便. 我想使用他的原因,暂时最主要是为了@import的功能.现在的问题:因为产品太多,是个页面,有10个css.而这10个cs ...
- 琐碎-同步centos集群的时间
想马上上手HBase,其对集群时间同步要求很高,当然,hadoop也是
- Android进阶笔记08:Android 中Activity、Window和View之间的关系
1. Android 中Activity.Window和View之间的关系(比喻): Activity像一个工匠(控制单元),Window像窗户(承载模型),View像窗花(显示视图) LayoutI ...
- MSSQL系统表常用操作
1:获取当前数据库中的所有用户表 select Name from sysobjects where xtype='u' and status>=0 2:获取某一个表的所有字段 select n ...
- Log4Net之初步了解
原创文章,转载必需注明出处:http://www.ncloud.hk/%E6%8A%80%E6%9C%AF%E5%88%86%E4%BA%AB/log4net%E4%B9%8B%E5%88%9D%E6 ...
- html的标签中 unselectable=on 属性的作用
在IE浏览器中,当input获得焦点时,点击有unselectable="on"属性的标签时,不会触发onblur事件. 加上该属性的元素不能被选中. < !DOCTYPE ...