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代码
################################属性 设置################################################字体设置{//-------- ...
- xml文件格式例如以下
xml文件格式例如以下: <?xml version="1.0" encoding="utf-8"?> <us ...
- android应用程序ANR定义
在Android上,如果你的应用程序有一段时间响应不够灵敏,系统会向用户显示一个对话框,这个对话框称作应用程序无响应(ANR:Application Not Responding)对话框.用户可以选择 ...
- javascript遍历数组
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/ ...
- 深入MySQL源码 学习方法 何登成专家
MYSQL 技术圈 有哪些做得好,又注重分享的公司: Oracle MySQL, MariaDB, Percona,Google, FB, Twitter, Taobao, NetEase… 有哪些值 ...
- c#_表单处理方式
阅读目录 开始 简单的表单,简单的处理方式 表单提交,成功控件 多提交按钮的表单 上传文件的表单 MVC Controller中多个自定义类型的传入参数 F5刷新问题并不是WebForms的错 以Aj ...
- SkyEye的使用
转载:http://blog.csdn.net/htttw/article/details/7226754 对于希望学习ARM汇编的同学而言, 购买ARM开发板进行板上实测无疑是一个有效的方法,不过购 ...
- 炼数成金hadoop视频干货04
视频地址:http://pan.baidu.com/s/1dDEgKwD 这一节讲的全是理论 任务执行优化 : 1.推测式执行: 2.重用JVM: 3.忽略模式. 除了手动修改Log4J.proper ...
- J - 病毒
J - 病毒 Time Limit:3000MS Memory Limit:131072KB 64bit IO Format:%lld & %llu Submit Status ...
- Fliptile
开关题 尺度法 Fliptile Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 3394 Accepted: ...