poj1068 Parencodings【模拟】【刷题计划】
Parencodings
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 27375 | Accepted: 16094 |
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
题意:给你t组数据,每组数据输入一个n,再输入n个数,表示第i个右括号的左边有pi个左括号,比如题目上的 4 5 6 6 6 6,第1个右括号的左边有4个左括号,第2个右括号的左边有5个左括号,以此类推,要求输出的是,第i个右括号的左边包含多少个已经与左括号匹配好的完整括号,比如题目上的1 1 1 4 5 6 ,在第1个右括号的左边有1个左括号与之匹配,所以第1个右括号的左边有1个完整匹配的括号,后边两个1也是这样推出来的,直到第4个右括号,它和从左到右数的第3个左括号匹配,而它们中间包括了3个已经匹配好的完整括号,最后加上自身匹配好的完整括号,总共有4个已经完整匹配好的括号。
(为神魔感觉我的翻译这么辣鸡啊。。嘤嘤嘤)
思路:模拟大法好咯。看到数据量比较小,我就放心大胆的用数组模拟好了,左括号为0,右括号为1,完整括号为-1,读入的时候,模拟括号的排列顺序,最后进行统计,将统计的完整括号匹配的值存入一个新的数组,最后输出就好啦。
统计-1的总数除以2才是完整括号数。
#include<stdio.h> #include<string.h> int main() { int str[50],num[50]; int t,n,i,j,number,count,sum; scanf("%d",&t); while(t--) { scanf("%d",&n); memset(str,0,sizeof(str)); for(i = 0; i < n; i ++) { scanf("%d",&number); str[number+i] = 1;//右括号处赋值为1 } sum = 0;//记录下标 for(i = 0; i < 2*n; i ++) { count = 0;//统计完整括号数 if(str[i]== 1)//遇到右括号时 { for(j = i; j >= 0; j --)//从遇到的右括号处向左统计,共有多少个完整括号 { if(str[j] == -1)//遇到完整括号,总数加1 count ++; if(str[j] == 0)//如果遇到与之匹配的左括号,结束循环 break; } count /=2;//总数除以2才是完整括号数 str[i] = str[j] = -1;//将匹配好的左右括号标记为-1 num[sum++] = count +1; //最后加上自身 } } for(i = 0; i < n-1; i ++) printf("%d ",num[i]); printf("%d\n",num[n-1]); } return 0; }