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 ...
随机推荐
- 栈的链式存储 - API实现
基本概念 其它概念详情參看前一篇博文:栈的顺序存储 - 设计与实现 - API实现 这里也是运用了链表的链式存储API高速实现了栈的API. 代码: // linkstack.h // 链式存储栈的A ...
- MySQL内存体系架构及参数总结 ---图解
http://www.cnblogs.com/kissdb/p/4009614.html 内存结构: Mysql 内存分配规则是:用多少给多少,最高到配置的值,不是立即分配 图只做大概参考 全局缓存包 ...
- [CodeForce]358D Dima and Hares
有N<3000只宠物要喂,每次只能喂一只,每喂一只宠物,宠物的满足度取决于: 1 紧靠的两个邻居都没喂,a[i] 2 邻居中有一个喂过了,b[i] 3 两个邻居都喂过了,c[i] 把所有宠物喂一 ...
- Qt 学习之路:输入元素
前面的章节中,我们看到了作为输入元素的MouseArea,用于接收鼠标的输入.下面,我们再来介绍关于键盘输入的两个元素:TextInput和TextEdit. TextInput是单行的文本输入框,支 ...
- hdu2660 Accepted Necklace (DFS)
Problem Description I have N precious stones, and plan to use K of them to make a necklace for my mo ...
- Objective-C--@property,@synthesize关键字介绍
Objective-C–@property,@synthesize关键字介绍 转载:http://www.cnblogs.com/QM80/p/3576282.html /** 注意:由@proper ...
- windows 下解决 Time_Wait 和 CLOSE_WAIT 方法
修改Time_Wait参数的方法 (在服务端修改)Windows下在HKEY_LOCAL_MACHINE/SYSTEM/CurrentControlSet/Services/Tcpip/Paramet ...
- HTTP协议基础与实验
一. HTTP协议(Hypetext Transfer Protoacal,超文本传输协议) HTTP协议规定了Web基本的运作过程,以及Web服务器之间的通信细节. Http协议采用客户端/服务器端 ...
- hibernate中有时候复杂删除有时候可以拆分为两个语句
这个demo是使用原生的sql语句写的,也就是没有调用我在struts中已经写好的公用类common中的增删改查功能,所以要开启事务
- (转)基于PHP的cURL快速入门
1. 原文:基于PHP的cURL快速入门 英文原文:http://net.tutsplus.com/tutorial ... for-mastering-curl/ 原文作者:Burak Guzel ...