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 ...
随机推荐
- delphi 插入表格HTML代码
<table width="174" height="76" border="1" align="center" ...
- Quart 2D 绘制图形简单总结
0 CGContextRef context = UIGraphicsGetCurrentContext(); 设置上下文 1 CGContextMoveToPoint 开始画线 2 CGConte ...
- C++ 模板类demo
#include <iostream> using namespace std; template <typename T> class MyVector { friend o ...
- mysql_upgrade命令
mysql 创建存储过程失败.查看错误日志,发现如下信息:*********************************************************************** ...
- Windows下用Git下载android源码 转载
http://my.oschina.net/jiadebin/blog/52631 1.首先你的电脑要安装好git,这个请参考git官网. 2.打开git命令窗口输入git clone http:// ...
- 装饰者模式--《Head First DesignPattern》
装饰者模式动态地将责任附加到对象杭,若要拓展功能,装设置提供了比继承更有弹性的替代方案. 星巴兹有多种咖啡,它们具有不同的价格.在购买咖啡时,也可以要求在其中加入各种调料,例如豆浆.摩卡.奶泡等等.需 ...
- SRM 584 第一次玩TopCoder。。。只水题一道。。。
第一次topcoder,以前老感觉没有资格去做tc,cf什么的,现在已经慢慢接触了. 感觉还可以,还是有让我们这些蒻菜安慰的水题. tc的确很好玩,用客户端比赛,还有各种规则,而且还是只编写一个类提交 ...
- CentOS(六)--Linux系统的网络环境配置
Linux系统下的网络环境配置,Linux.Unix就是网络的世界,所以在Linux系统中如何配置网络环境变量是至关重要的,这里将会给出3种Linux系统下网络环境配置的方法! 在配置网络环境之前,首 ...
- Adobe Edge Animate –解决图形边缘精确检测问题-通过jquery加载svg图片
Adobe Edge Animate –解决图形边缘精确检测问题-通过jquery加载svg图片 版权声明: 本文版权属于 北京联友天下科技发展有限公司. 转载的时候请注明版权和原文地址. 在edge ...
- JavaScript基础笔记二
一.函数返回值1.什么是函数返回值 函数的执行结果2. 可以没有return // 没有return或者return后面为空则会返回undefined3.一个函数应该只返回一种类型的值 二.可变 ...