POJ 1068 -- Parencodings
Posted Amysear
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了POJ 1068 -- Parencodings相关的知识,希望对你有一定的参考价值。
Parencodings
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 27602 | Accepted: 16226 |
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:
Write a program to convert P-sequence of a well-formed string to the W-sequence of the same string.
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
Source
题意:
对于给出的原括号串,存在两种数字密码串:
1.p序列:当出现匹配括号对时,从该括号对的右括号开始往左数,直到最前面的左括号数,就是pi的值。
2.w序列:当出现匹配括号对时,包含在该括号对中的所有右括号数(包括该括号对),就是wi的值。
题目的要求:对给出的p数字串,求出对应的w序列。
解题思路:
模拟题
给的p序列长度n(1 <= n <= 20)即为右括号的个数。
在处理括号序列时可以使用一个小技巧,把括号序列转化为01序列,左0右1,处理时比较方便而01序列的长度也不会超过40。
1 #include<iostream> 2 using namespace std; 3 int p[21]; 4 int s[41]; 5 int w[21]; 6 int main() 7 { 8 int n; 9 cin>>n; 10 while(n--) 11 { 12 int len; 13 cin>>len; 14 for(int i=0;i<len;i++) 15 { 16 cin>>p[i]; 17 } 18 int sj=0; 19 int leftCount = 0; 20 for(int i=0;i<len;i++) 21 { 22 for(int j=0;j<(p[i] - leftCount);j++) 23 { 24 s[sj] = 0; 25 sj++; 26 } 27 leftCount = p[i]; 28 s[sj] = 1; 29 sj++; 30 } 31 /* 32 //测试s序列代码 33 for(int i=0;i<len*2;i++) 34 cout<<s[i]; 35 cout<<endl; 36 */ 37 int rightCount = 0; 38 while(rightCount<len) 39 { 40 int temp = 0; 41 for(int i=0;i<len *2;i++) 42 { 43 if(s[i] == 1) temp++; 44 if(temp>rightCount) 45 {///找到当前没有输出的第一个右括号,位置为i 46 int right = 0;int left = 0; 47 for(int j=i;j>=0;j--) 48 {//从第i个位置向前遍历,右括号计数为right,左括号计数为left 49 //当left == right 时,停止遍历,找到了当前右括号的左括号,进行输出 50 if(s[j] == 1) right++; 51 else{ 52 left++; 53 if(left == right) 54 { 55 cout<<left; 56 if(temp != len) cout<<" "; 57 else cout<<endl; 58 break; 59 } 60 } 61 } 62 //更新当前找到的右括号的个数 63 rightCount = temp; 64 } 65 } 66 } 67 } 68 return 0; 69 }
以上是关于POJ 1068 -- Parencodings的主要内容,如果未能解决你的问题,请参考以下文章