博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Parencodings
阅读量:6265 次
发布时间:2019-06-22

本文共 2471 字,大约阅读时间需要 8 分钟。

Problem 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 1456
Write 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 9
 
Sample Output
1 1 1 4 5 6
1 1 2 4 5 1 1 3 9
 

题意:一行括号数列,分别给出第i个右括号前有多少个左括号;要求求出与第i个右括号相对应的左括号所形成的括号对中包括多少左括号(包括本身与第i个右括号对应的左括号)

输入:

第一行:输入一个数t,表示有t组数列需要处理;接下来的2*t行,没两行是一组需要处理的数据,上一行第n表示有n个右括号;接下来的一行有n个整数;

AC代码:

1 #include
2 #include
3 #include
4 #include
5 6 7 8 using namespace std; 9 10 11 int main()12 {13 freopen("1.txt","r",stdin);14 int dp[50]={
0};//模拟数组,初始值都为0;15 int n,num,i;16 int s,j;17 int sum;18 cin>>num;//输入样例的个数;19 while(num)20 {21 22 cin>>n;//输入右括号的个数;23 for(i=0;i
>j;25 dp[j+i]=1;26 }27 s=n;28 n<<=1;29 i=0;30 while(s){31 while(dp[i++]<=0);32 sum=0;33 for(j=i-2;j>=0;j--)34 {35 if(dp[j]<=0)sum++;//确定了一个左括号;36 if(dp[j]==0){dp[j]=-1;break;}//到了与相应右括号对应的左括号处,跳出循环。37 }38 cout<
<<" ";//输出左括号的个数。39 s--;40 }41 cout<
View Code

 

转载于:https://www.cnblogs.com/zhangchengbing/p/3226638.html

你可能感兴趣的文章
【Cocos2d-x游戏开发】细数Cocos2d-x开发中那些常用的C++11知识
查看>>
otl使用存储过程或是LEFT JOIN时提示输出类型未知的问题
查看>>
集群(cluster)原理(转)
查看>>
小数格式:
查看>>
【MyBatis学习06】_parameter:解决There is no getter for property named in class java.lang.String...
查看>>
Eclipse导入别人的项目报错:Unable to load annotation processor factory 'xxxxx.jar' for project...
查看>>
与孩子一起学编程10章
查看>>
【再探backbone 03】博客园单页应用实例(提供源码)
查看>>
android 圆角编写(懒得去找,写给自己看的)
查看>>
chrome 搜索 jsonView
查看>>
chrome浏览器:chrome 69 恢复默认UI
查看>>
Irony - 一个 .NET 语言实现工具包
查看>>
Java之Static静态修饰符详解
查看>>
修改weblogic部署的应用名称
查看>>
aaronyang的百度地图API之LBS云与.NET开发 Javascript API 2.0【基本地图的操作】
查看>>
Java Nio 多线程网络下载
查看>>
C++不让程序一闪而过
查看>>
C# 中的枚举类型 enum (属于值类型)
查看>>
[Debug] Use Snippets to Store Behaviors in Chrome DevTools
查看>>
【Java面试题】3 Java的"=="和equals方法究竟有什么区别?简单解释,很清楚
查看>>