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 ...
随机推荐
- Debian自启动知识 2015-03-31 20:23 79人阅读 评论(0) 收藏
Debian6添加了insserv用来代替update-rc.d.update-rc.d 就不多做介绍. Debian6里边要添加一个自动启动的服务需要先将启动脚本放在/etc/init.d,然后使用 ...
- 《Java并发编程实战》第六章 任务运行 读书笔记
一. 在线程中运行任务 无限制创建线程的不足 .线程生命周期的开销很高 .资源消耗 .稳定性 二.Executor框架 Executor基于生产者-消费者模式.提交任务的操作相当于生产者.运行任务的线 ...
- 动态设置布局LayoutInflater
LayoutInflater作用是将layout的xml布局文件实例化为View类对象.LayoutInflater 的作用类似于 findViewById(),不同点是LayoutInflater是 ...
- android 10 事件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layo ...
- /proc/sys/ 下内核参数解析
http://blog.itpub.net/15480802/viewspace-753819/ http://blog.itpub.net/15480802/viewspace-753757/ ht ...
- AndroidStudyDemo之Android4.x介绍
Android4.x 思维导图 作者:李旺成 时间:2016年4月7日 AndroidStudyDemo 系列篇章开始了!!! AndroidStudyDemo 你值得拥有AndroidStudyDe ...
- ssh登录很慢解决方法
使用ssh客户端(如:putty)连接Linux服务器,可能会等待10-30秒才有提示输入密码.严重影响工作效率.登录很慢,登录上去后速度正常,这种情况主要有两种可能的原因: 1. DNS反向解析问题 ...
- svn 设置钩子将代码同步到web目录下面
首先:确定思路: 要在SVN服务中,找到仓库文件夹的位置,在相应的项目中找到hooks文件夹.在该文件中添加一个post-commit文件: 当有commit动作发生时(提交到SVN服务是就会执行这个 ...
- 手势交互之GestureOverlayView
一种用于手势输入的透明覆盖层,可以覆盖在其他空间的上方,也可包含在其他控件 android.gesture.GestureOverlayView 获得手势文件 需要用GesturesBuilder,如 ...
- android menu菜单自动生成
Android提供了一些简单的方法来为应用添加Menu菜单. 提供了三种类型应用菜单: 一.Options Menu:通过Menu按钮调用菜单 1.在/res/目录下新建menu文件夹,用于存储Men ...