Parencodings
原题链接
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 27644 Accepted: 16253
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
Source
Tehran 2001
Parencodings
[原题链接](http://poj.org/problem?id=1068)
时间限制:1000MS内存限制:10000K
提交总数:27644录取时间:16253
描述
让S = s1 s2 … s2n是一个格式良好的括号串。 S可以用两种不同的方式编码:
q通过整数序列P = p1 p2 … pn其中pi是S(P序列)中第i个右括号之前的左括号数。
q对整数序列W = w1 w2 … wn其中对于每个右括号,例如在S中说a,我们关联一个整数,该整数是从a的匹配左括号开始计算的右括号的数目。 (W-序列)。
以下是上述编码的示例:
S (((()()())))
) ) ))))
P序列 4 5 6 6 6 6
W序列 1 1 1 4 5 6
编写一个程序,将格式良好的字符串的P序列转换为相应的字符串的W序列。
输入
输入的第一行包含一个整数t(1 <= t <= 10),即测试用例的数量,后面是每个测试用例的输入数据。每个测试用例的第一行是一个整数n(1 <= n <= 20),第二行是格式正确的字符串的P序列。它包含n个正整数,用空格分隔,表示P序列。
输出
输出文件完全由对应于测试用例的t行组成。对于每个测试用例,输出行应该包含n个整数,用于描述对应于其给定P序列的字符串的W序列。
示例输入
2
6
4 5 6 6 6 6
9
4 6 6 6 6 8 9 9 9
示例输出
1 1 1 4 5 6
1 1 2 4 5 1 1 3 9
来源
德黑兰2001年
题意
这题还是比较好理解的,就是给你一个序列,这个序列是根据一个括号对组成的字符串构造的:第 i 个元素表示第 i 个右括号左边全部左括号个数。然后你需要根据这个给出序列构造另一个新序列。这个新序列也可以通过刚才字符串来构造:表示第 i 个元素表示第 i 个右括号到和它匹配的左括号之间的右括号个数(包括那个右括号)。
题解
根据第一个序列可以把第二个序列构造出来,然后再根据字符串构造第二个序列。
代码
#include<cstdio>
const int maxn=10005;
int n,m,p,lst,st[maxn],r[maxn],pir[maxn],s[maxn<<1];
//为了方便统计,把字符串中的右括号看成1,左括号看成0,然后造前缀和(s数组)最后直接输出两点前缀和
//之差。pir[i]表示第i个右括号对应的左括号在字符串上的位置;r[i]表示第i个右括号在字符串上的位置;
//st数组是栈,用来辅助构造pir。
int read()//读优
{
int ret=0,f=1;char ch=getchar();
while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
while (ch>='0'&&ch<='9') ret=ret*10+ch-'0',ch=getchar();
return ret*f;
}
int main()
{
int T=read();
while (T--)
{
n=read();m=lst=st[0]=s[0]=0;
for (int i=1;i<=n;i++)
{
p=read();
for (int j=1;j<=p-lst;j++) st[++st[0]]=++m,s[m]=s[m-1];
pir[i]=st[st[0]--];s[++m]=s[m-1]+1;r[i]=m;lst=p;
}
for (int i=1;i<=n;i++) printf("%d%c",s[r[i]]-s[pir[i]],i==n?'\n':' ');
//本来应该输出s[r[i]]-s[pir[i]-1]的,但是由于s[pir[i]]==s[pri[i]-1](因为pir[i]位置上的肯定是左括号)和前面一个肯定相等。
}
return 0;
}