Poj1068:

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

题目大意:这里有三个数组S、P、W,S是长度为2n是符合数学规律的括号字符串,P、W是依据S生成的长度为n的整数列。

P:{p1,p2,p3…}第i个右括号左边的左括号数目,记为pi.

W:{w1,w2,w3…}第i个右括号与其匹配的左括号,截取的那个区间段左括号的数目。

现在给出P序列,求出对应的W序列。

数理分析:很容易想到,首先我们应该根据P得到S,然后根据S和W的定义得到W。因此整个模拟流程分为如下的两个步骤:

(一)P -> S:

遍历整数列P,其含义是第i个右括号左边有的左括号数目,我们先将n个右括号画出,则第i个右括号和第i-1个右括号中间隔了P[i] – P[i-1]个左括号,按照这个规律,我们遍历P[i],先生成P[i]-P[i-1]个左括号(P[0] = 0),然后生成一个右括号,便可构造出S。

(二)S –> W:

我们遍历S,设置数组left[i]记录第i个左括号在字符串数组S中的下标,则我们在遍历过程中一遇到右括号,就应该和left数组的尾部元素匹配,然后将left数组的尾部元素删除。(其实模拟了一个栈过程),此时我们知道尾部元素在S中的下标和右括号在S中的下标,不难得出这之间有多少个左括号。

简单的参考代码如下:

 #include<cstdio>

#include<cstring>

using namespace std;

const int maxn = ;

int main()

{

    char s[maxn];

    int p[maxn];

    int w[maxn];

    int t,index;

    scanf("%d",&t);

    while(t--)

    {

        int n;

        scanf("%d",&n);

        for(int i = ;i <= n;i++)

              scanf("%d",&p[i]);

        index = ;

        p[] = ;

        for(int i = ;i <= n;i++)

        {

             int temp = p[i] - p[i-];

              while(temp--)

              {

                  s[index++] = '(';

              }

                 s[index++] = ')';

        }   //得到s序列

        n *= ;//得到w序列

        index = ;

        int left[maxn];

        bool left2[ maxn];

        memset(left2 ,false , sizeof(left2));

        for(int i = ;i < n;i++)

        {

              if(s[i] == '(')

                    left[index++] = i;

              else

                 {

                   if(i == n-) {printf("%d\n" ,(i-left[--index]+)/);}

                   else      {printf("%d ",(i-left[--index]+)/);}

                 }

        }

    }

}

《ACM国际大学生程序设计竞赛题解I》——6.8的更多相关文章

  1. 《ACM国际大学生程序设计竞赛题解Ⅰ》——基础编程题

    这个专栏开始介绍一些<ACM国际大学生程序设计竞赛题解>上的竞赛题目,读者可以配合zju/poj/uva的在线测评系统提交代码(今天zoj貌似崩了). 其实看书名也能看出来这本书的思路,就 ...

  2. 《ACM国际大学生程序设计竞赛题解I》——6.10

    Pku 1143: Description Christine and Matt are playing an exciting game they just invented: the Number ...

  3. 《ACM国际大学生程序设计竞赛题解I》——6.11

    pku 1107: Description Weird Wally's Wireless Widgets, Inc. manufactures an eclectic assortment of sm ...

  4. 《ACM国际大学生程序设计竞赛题解Ⅰ》——模拟题

    这篇文章来介绍一些模拟题,即一类按照题目要求将现实的操作转换成程序语言. zoj1003: On every June 1st, the Children's Day, there will be a ...

  5. 2018 ACM 国际大学生程序设计竞赛上海大都会部分题解

    题目链接 2018 ACM 国际大学生程序设计竞赛上海大都会 下午午休起床被同学叫去打比赛233 然后已经过了2.5h了 先挑过得多的做了 .... A题 rand x*n 次点,每次judge一个点 ...

  6. 2018 ACM 国际大学生程序设计竞赛上海大都会赛

    传送门:2018 ACM 国际大学生程序设计竞赛上海大都会赛 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛2018-08-05 12:00:00 至 2018-08-05 17:00:0 ...

  7. 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 F Color it

    链接:https://www.nowcoder.com/acm/contest/163/F 来源:牛客网 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 F Color it 时间限制:C ...

  8. 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 F Color it (扫描线)

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 F Color it (扫描线) 链接:https://ac.nowcoder.com/acm/contest/163/F来源:牛客网 时间 ...

  9. 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP)

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 J Beautiful Numbers (数位DP) 链接:https://ac.nowcoder.com/acm/contest/163/ ...

随机推荐

  1. siege安装和使用

    siege(支持http.https).多url. 下载地址:http://www.joedog.org/index/siege-home Cent os系统: 1)./configure 2)mak ...

  2. Creating a web application.

    About creating web GIS applications As you learn and use ArcGIS for Server, you'll probably reach th ...

  3. 数据库连接报错之IO异常(The Network Adapter could not establish the connection)

    Io 异常: The Network Adapter could not establish the connection 有以下四个原因: 1.oracle配置 listener.ora 和tnsn ...

  4. 06MySQL数据库入门

    1.数据库的概念 数据库是保存数据的仓库,可以方便的把数据放进去,并且把数据根据各种需求取出来. 数据库管理系统(Database Management System,DBMS)是对数据库进行管理(增 ...

  5. Unity 5.x---00使用重力

    Unity 5.x---00使用重力 步骤一: 打开一个工程(导入Unity自带的资源),并创建并配置好必要的GameObject ,如下图: 步骤二: 1.创建一个Cube,使其位于平面上方.    ...

  6. javascript——闭包

    <script type="text/javascript"> //什么是闭包: //是指语法域位于某个特定的区域,具有持续参照(读写)位于该区域内自身范围之外的执行域 ...

  7. PAT - 基础 - 最大公约数和最小公倍数

    题目: 本题要求两个给定正整数的最大公约数和最小公倍数. 输入格式: 输入在一行中给出2个正整数M和N(<=1000). 输出格式: 在一行中顺序输出M和N的最大公约数和最小公倍数,两数字间以1 ...

  8. PHP 用户注册

    注册页面 reg.html 负责收集用户填写的注册信息.教程里只列出关键的代码片段,完整的代码附在本节最后. 注册表单 <fieldset> <legend>用户注册</ ...

  9. mysql数据类型——TEXT和Blob

    TEXT是 以文本方式存储的,如果存储英文的话区分大小写  Blob是以二进制方式存储的,不区分大小写. xxxBlob存储的数据只能整体读出 有4种text类型:tinytext.text.medi ...

  10. yum版本新增包的一般步骤

    在Jekins的自动构建环境中,有时会有在构建出的ISO中添加新应用app需求,对于采用rpm包源代码管理方式的构建环境来说,基本步骤如下: 1.下载app的src.rpm包 2.解压src.rpm包 ...