poj 1068 Parencodings/bnuoj 1187 Parencodings 解题报告

来源:互联网 发布:去泰山捡石头必知 编辑:程序博客网 时间:2024/06/06 04:50

之前在做题的时候一直不会,总会依赖网上的解题报告。久而久之发现这个办法实在不可行,进步超级的慢。终于下定决心不再依赖网上的解题报告,自己慢慢想,慢慢做。

以菜鸟的身份自然要先找水题来做啦。由于“=”勿写成了“==”,就在这到题上耗了一天,谨此作为纪念吧。

题目链接:

http://www.bnuoj.com/bnuoj/problem_show.php?pid=1187

http://poj.org/problem?id=1068

题目:

Parencodings

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
264 5 6 6 6 69 4 6 6 6 6 8 9 9 9
Sample Output
1 1 1 4 5 61 1 2 4 5 1 1 3 9

题意:

设序列 S 是一个完全匹配的括号序列。序列 S 能用下面两种不同方法加密:通过一个整数序列 P = p1 p2...pn  ,其中 pi 表示序列 S 中第 i 个右括号 ")" 之前的左括号 “(” 的数量。当然这些括号是从左到右数的。通过一个整数序列 W = w1 w2...wn ,wi 表示从第 i 个右括号 ")" 相匹配的左括号 “(” 的位置开始数的右括号 “)” 的数目,包括第 i 个右括号 “)” 本身。 下面是上面两种加密方法的例子:S    (  (  (  (  )  (  )  (  )  )  )  )P-sequence                4     5    6 6 6 6
W-sequence    1 1 1456
解释:第一个右括号 ")" 出现在 S 序列的第5个位置上,他前面有 4个左括号 “(” ,因此 p1 = 4解释:第一个右括号 ")" 出现在 S 序列的第5个位置上,和他匹配的左括号 “(” 出现在S 序列的第4个位置上,从第4个位置开始数到第5个位置,有 1个右括号 ")" ,因此 w1 = 1 。第二个右括号 ")" 出现在 S 序列的第7个位置上,和他匹配的左括号 “(” 出现在S 序列的第6个位置上,从第6个位置开始数到第7个位置,有 1个右括号 ")" ,因此 w2 = 1 。第三个右括号 ")" 出现在 S 序列的第9个位置上,和他匹配的左括号 “(” 出现在S 序列的第8个位置上,从第8个位置开始数到第9个位置,有 1个右括号 ")" ,因此 w3 = 1 。第四个右括号 ")" 出现在 S 序列的第10个位置上,和他匹配的左括号 “(” 出现在S 序列的第3个位置上,从第3个位置开始数到第10个位置,有 4个右括号 ")" ,因此 w4 = 4 。第五个右括号 ")" 出现在 S 序列的第11个位置上,和他匹配的左括号 “(” 出现在S 序列的第2个位置上,从第2个位置开始数到第11个位置,有 5个右括号 ")" ,因此 w5 = 5 。第六个右括号 ")" 出现在 S 序列的第12个位置上,和他匹配的左括号 “(” 出现在S 序列的第1个位置上,从第1个位置开始数到第12个位置,有 6个右括号 ")" ,因此 w6 = 6 。
解题思路:
1、模拟括号,将括号存入字符串数组中。
2、遍历字符串数组,遇到“)”则向前找与之匹配的“(”.
3、判断“(”是否已经被匹配过,用visit数组来标记
#include<iostream>#include<cstring>#include<string>using namespace std;int main(){    int cas, n, cnt;//组数,个数,w    int p[40], visit[40];    string str;    cin >> cas;    while(cas --)    {        cin >> n;        memset(visit,0,sizeof(visit));        for(int i = 1; i <= n; i ++)//模拟括号        {            cin >> p[i];            p[0] = 0;            if(p[i] - p[i - 1] != 0)            {                for(int j = 0;j < p[i] - p[i - 1] ;j ++ )                {                    str+='(';                }                str+=')';            }            else            {                str+=')';            }        }        //遇到有括号就向前找最近的左括号        //用visit数组标记是否访问过左括号        for(int i = 0; i < str.size() ;i++ )        {            cnt = 0;            if(str[i] == ')')            {                visit[i] = 1;                for(int j = i; j > 0; j--)                {                    if(str[j] == '(' && visit[j] == 0)                    {                        visit[j] = 1;                        break;                    }                    else if(str[j] == '(' && visit[j] == 1)                    {                        cnt ++;                    }                }                cout<<cnt+1<<" ";            }        }        str.clear();//清零        cout<<endl;    }    return 0;}


原创粉丝点击