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. Android开发常见错误类型一览表

    这是我的第一个博客,我会一直添加我在Android开发中遇到的错误,用来记录我开发中踩过的那些坑 ------------------------分割线------------------------ ...

  2. XCODE6 提交至 App Store

    新到一个公司,以前的苹果开发人员离职,临时接手他的苹果代码,需要修改并上线到APP STORE.  xcode6.0升级到最新的6.1后, 发现各种坑 1. 路径配置不对, 这个是个人习惯问题,之前的 ...

  3. Error Creating Deployment 有关Tomcat配置问题

    配置Tomcat的时候出现提示框The selected server is enabled,but is not configured properly.Deployment to it will ...

  4. 《Python 二三事》——python学习必看(转载)

        面向初学者介绍Python相关的一些工具,以及可能遇到的常见问题. 原文出处 原文作者:八八年出生的男性,互联网上常用id是 jagttt .目前正从事 IT 行业的工作.业余爱好是动漫游加电 ...

  5. script加defer="defer" 的意义

    <script defer="defer">alert("页面加载完我才执行的")</script>先看到这段话 然后再执行上面的 JS ...

  6. Window.onload事件

    window.onload是一个事件,当文档加载完成之后就会触发该事件,可以为此事件注册事件处理函数,并将要执行的脚本代码放在事件处理函数中,于是就可以避免获取不到对象的情况

  7. js文字向上滚动代码

    js文字向上滚动代码 <style>.pczt_pingfen_jhxs_news1{ width:397px;  background:#edfafd; padding-top:2px; ...

  8. php之文件上传类代码

    /* 单个文件上传 功能 上传文件 配置允许的后缀 配置允许的大小 获取文件后缀 判断文件的后缀 报错 */ class UpTool{ protected $allowExt = 'jpg,jpeg ...

  9. pycharm常用快捷键与设置

    pycharm高频率使用的快捷键 Ctrl+Shift+F10 运行当前的页面 Ctrl + / 注释(取消注释)选择的行 Ctrl+Shift+F 高级查找 Shift + Enter 开始新行 T ...

  10. openerp service install

    cp  openerp.init  to   /etc/init.d/openerp update-rc.d   openerp default PATH=/sbin:/usr/sbin:/bin:/ ...