Parencodings
Description
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
Output
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 这个题理解了题意就不难了
输入2 6 不解释
4 5 6 6 6 6
可以这样理解
(((()()())))
第一个右括号左边有4个左括号
第二个右括号左边有5个左括号
以此类推;
所以根据所给出的数字串;很容易还原括号;
输出时:(((()()()))) 一个完整的()输出1;共3个
(()()())这个输出4一个()有3个(),3+1; 一句话就是
比如(((()()()))),
输入:每个右括号之前的左括号数序列为P=4 5 6 6 6 6,;
输出:每个右括号所在的括号内包含的括号数为W=1 1 1 4 5 6. 可以利用 栈和队列
#include<iostream>
#include<queue>
#include<stack>
#include<cstdio>
using namespace std;
int main()
{
queue<char>p,q;
stack<char>Q;
int a,b,c,m,n;
cin>>n;
while(n--)
{
cin>>m;
b=;
while(m--)
{
cin>>a;
c=a-b;//制定压入的左括号的数目
b=a;
while(c--)
p.push('(');
p.push(')');//每次压入左括号之后压入一个右括号
}
while(!p.empty())//将队列中的括号一个个取出
{
if(p.front()=='(')
Q.push(p.front());//如果是左括号直接压入栈中 else if(p.front()==')')//如果是右括号,则进行判断
{
if(Q.top()=='(')//如果栈顶元素是左括号
{
Q.pop();//出栈
Q.push();//压入 1
q.push();//这是为了把数字储存下来,方便以后输出答案
}
else//如果栈顶元素不是左括号
{
int sum=;
while(Q.top()!='(')//持续出栈 直到左括号
{
sum+=Q.top();//统计一共包含多少左括号
Q.pop();
}
Q.pop();//左括号出栈
q.push(sum);//这是为了把数字储存下来,方便以后输出答案
Q.push(sum);//压入括号个数
}
}
p.pop();
}
printf("%d",q.front());
q.pop();
while(!q.empty())
{
printf(" %d",q.front());
q.pop();
}
printf("\n");
}
return ;
}
Parencodings的更多相关文章
- [POJ1068]Parencodings
[POJ1068]Parencodings 试题描述 Let S = s1 s2...s2n be a well-formed string of parentheses. S can be enco ...
- Parencodings(imitate)
Parencodings Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 20679 Accepted: 12436 De ...
- 模拟 POJ 1068 Parencodings
题目地址:http://poj.org/problem?id=1068 /* 题意:给出每个右括号前的左括号总数(P序列),输出每对括号里的(包括自身)右括号总数(W序列) 模拟题:无算法,s数组把左 ...
- Parencodings 分类: POJ 2015-06-28 22:00 7人阅读 评论(0) 收藏
Parencodings Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 22757 Accepted: 13337 De ...
- hdu 1361 Parencodings 简单模拟
Parencodings 题意: 由括号序列S可经P规则和W规则变形为P序列和W序列. p规则是:pi是第i个右括号左边的左括号的数: w规则是:wi是第i右括号与它匹配的左括号之间右括号的数(其中包 ...
- POJ 1068 Parencodings
Parencodings Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 24932 Accepted: 14695 De ...
- Poj OpenJudge 1068 Parencodings
1.Link: http://poj.org/problem?id=1068 http://bailian.openjudge.cn/practice/1068 2.Content: Parencod ...
- POJ1068——Parencodings
Parencodings Description Let S = s1 s2...s2n be a well-formed string of parentheses. S can be encode ...
- PO1068 Parencodings 模拟题
Parencodings Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 28860 Accepted: 16997 De ...
- Hdu1361&&Poj1068 Parencodings 2017-01-18 17:17 45人阅读 评论(0) 收藏
Parencodings Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total ...
随机推荐
- BFS-hdu-4101-Ali and Baba
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=4101 题目大意: 给一个矩阵,0表示空的可走,-1宝藏的位置(只有一个),其余的正整数表示该位置石头 ...
- Struts 有哪些经常使用标签库
Struts 有哪些经常使用标签库 1.html标签库 2.bean标签库 3.logic标签库
- [RxJS] Filtering operators: skipWhile and skipUntil
After takeUntil() and takeWhile() function, let's have a look on skipWhile() and skilUntil() functio ...
- block没那么难(一):block的实现
本系列博文总结自<Pro Multithreading and Memory Management for iOS and OS X with ARC> block 顾名思义就是代码块,将 ...
- 走进 Facebook POP 的世界
POP: 一个流行的可扩展的动画引擎iOS,它支持spring和衰变动态动画,使其可用于构建现实,基于物理交互.Objective - C API允许快速集成, 对于所有的动画和过渡他是成熟的. 解释 ...
- Java的演变过程
1. 1996.01.23 JDK1.0 代号Oak:212个类.8个包: 2. 1997.02.19 JDK1.1 504个类.23个包: Java Bean.远程方法调用(RMI).JAR文件格式 ...
- LiLei&HanMeiMei的隐式马尔可夫爱情
一篇非常棒的隐马尔可夫入门文章...推荐! from: http://staffwww.dcs.shef.ac.uk/people/W.Liu/hmm.html
- 关于c++中的引用
引用是个别名. 1.引用是否占用空间 引用是否占用空间,此处是指广义上的占用内存空间,即为该对象新开辟一块内存.这个需要分不同的情况. 首先看一下常引用(const 引用). 这里关于常引用在c++ ...
- hadoop之wordCount程序理解
有篇文章http://www.cnblogs.com/xia520pi/archive/2012/05/16/2504205.html中介绍的
- HTML 5 Audio/Video DOM buffered 属性
1.实例1获取视频第一段缓冲范围部分,以秒计: myVid=document.getElementById("video1"); alert("Start: " ...