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 ...
随机推荐
- [Angular 2] Inject Service with "Providers"
In this lesson, we’re going to take a look at how add a class to the providers property of a compone ...
- 一个公网地址部署LVS/DR模式
http://blog.chinaunix.net/uid-7411781-id-3436142.html 一个公网地址部署LVS/DR模式 网上看了很多关于LVS的文章,在选取2种模式LVS/D ...
- 《Linux内核设计与实现》读书笔记
http://www.cnblogs.com/wang_yb/tag/linux-kernel/
- 隐藏gvim中的工具栏和菜单栏
在vim的配置文件.vimrc中添加如下代码: "Toggle Menu and Toolbar set guioptions-=m set guioptions-=T map <si ...
- cocos2dx libjson
libjson下载 http://sourceforge.net/projects/libjson/ 下载解压后改名成libjson,用到的是根目录下面的JSONOptions.h.libjson.h ...
- divide-conquer-combine(4.1 from the introduction to algorithm)
this example is from chapter 4 in <the introduction to algorithm> the main idea is all showed ...
- Linux内核初始化定义
转载:http://blog.csdn.net/beatbean/article/details/8448623 1. Compile宏控制 位于include/linux/init.h /* The ...
- Asp.Net 之 WebService部署到服务器后出现" The test form is only available for requests from the local machine "
最近由于任务需要开发了一个WebService, 部署到服务器以后,出现上述问题,网上查找到如下解决方案: 问题原因: 从 NET Framework 1.1 起定义了一个名为 HttpPostLoc ...
- Balanced Lineup(最简单的线段树题目)
Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 33389 Accepted: 15665 Case Time Limit ...
- [改善Java代码]断言绝对不是鸡肋
建议19: 断言绝对不是鸡肋 在防御式编程中经常会用断言(Assertion)对参数和环境做出判断,避免程序因不当的输入或错误的环境而产生逻辑异常,断言在很多语言中都存在,C.C++.Python都有 ...