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. org.springframework.beans.factory.BeanCreationException: 求教育!

    2014-11-26 14:05:56 [org.springframework.web.context.support.XmlWebApplicationContext]-[WARN] Except ...

  2. 【转】 NSString什么时候用copy,什么时候用strong

    原文: http://blog.csdn.net/itianyi/article/details/9018567 大部分的时候NSString的属性都是copy,那copy与strong的情况下到底有 ...

  3. 第五篇、 WebSphere8.5的安装

    一.前言 WebSphere Application  Server 是IBM企业级应用服务器,与WAS6,WAS7相比较而言 WAS8发生了很大的改变,其安装介质和以前截然不同,该篇章中对于不同的安 ...

  4. java中jdk环境配置

    配置java环境,俗称jdk环境 首先进入配置环境的目录下:右键鼠标我的电脑->属性->高级系统设置->环境变量,在对应的"系统变量"框下配置一下变量: 规范的配 ...

  5. smarty 的学习----ubuntu下初步配置

    转自:http://blog.csdn.net/ma332567575/article/details/7904124 首先去www.smarty.net下载最新版的Smarty 把下载后的压缩包在网 ...

  6. phpexcel 一些基本的设置 (表格的基本属性)

    网址是:http://www.thinkphp.cn/code/1893.html

  7. php基础知识【oop/mvc/orm/aop】

    OOP 面向对象编程是一种计算机编程架构.OOP 的一条基本原则是计算机程序是由单个能够起到子程序作用的单元或对象组合而成.OOP 达到了软件工程的三个主要目标:重用性.灵活性和扩展性.为了实现整体运 ...

  8. TRECT的使用

    作为一张画布,在上面绘制各种图形或显示图像,但在CANVAS的使用过程中少不了一个特殊对象,那就是矩形RECT,灵活使用它会完成很多特殊的功能,为Delphi编制的Windows程序增加活力. REC ...

  9. Latex及Beamer

    一 资源 Latex编辑部 Codecogs,latex在线编辑可见

  10. iOS中MVC等设计模式详解

    iOS中MVC等设计模式详解 在iOS编程,利用设计模式可以大大提高你的开发效率,虽然在编写代码之初你需要花费较大时间把各种业务逻辑封装起来.(事实证明这是值得的!) 模型-视图-控制器(MVC)设计 ...