zoukankan      html  css  js  c++  java
  • ZOJ Problem Set – 1016 Parencodings

    原题:

    Parencodings


    Time Limit: 1 Second      Memory Limit: 32768 KB


    Let S = s1 s2 ... s2n be a well-formed string of parentheses. S can be encoded in two different ways:

    • 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).
    • 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 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: Asia 2001, Tehran (Iran)

    虽然是一道简单题,但是由于是英文的,我做的时候理解起来还是花了那么一段时间,现在分析一下,主要是理解一下题目,让我自己以后看的时候不要摸不到头脑就可以了。

    题意是这样的,现在有一系列的(),要对这些左右圆括号进行编码。现在有两种编码形式,第一种:从给定的S(左右圆括号串)中,从左往右说,找到一个右括号,记录在这个右括号之前出现的左括号的数量,即P编码。第二种,还是从给定串的左边开始往右数,找到右括号,然后从这个位置往左数,记录与之匹配的第一个左括号是出现的第几个左括号,即W编码。例如:

    S (((()()())))
    P-sequence 4 5 6666
    W-sequence 1 1 1456

    然后,题目给出了已知S串的P编码形式,求其W编码串。虽然根据P编码形式可以反向求得原S串,但是这却不是最佳解法。其实我们是可以根据P串直接求W串的。

    好啦,题意理解清楚了,就可以给出解题代码了。

      1: #include<iostream>
    
      2: #include<vector>
    
      3: using namespace std;
    
      4: 
    
      5: int main(void)
    
      6: {
    
      7:     int times;
    
      8:     while(cin>>times)
    
      9:     {
    
     10:         for(int items;times && cin>>items;times--)
    
     11:         {
    
     12:             vector<int> leftParenthsis(items,0);
    
     13:             int preNumber = 0;
    
     14:             for(int numbers,i = 0;i < items && cin>>numbers; i++)
    
     15:             {
    
     16:                 leftParenthsis[i] = numbers - preNumber;
    
     17:                 preNumber = numbers;
    
     18:                 cout<<(i ? " ":"");
    
     19:                 if(leftParenthsis[i])
    
     20:                 {
    
     21:                     cout<<1;
    
     22:                     leftParenthsis[i]--;
    
     23:                 }
    
     24:                 else
    
     25:                 {
    
     26:                     for(int j = i; j >= 0;j--)
    
     27:                     {
    
     28:                         if(leftParenthsis[j])
    
     29:                         {
    
     30:                             cout<<i - j + 1;
    
     31:                             leftParenthsis[j]--;
    
     32:                             break;
    
     33:                         }
    
     34:                     }
    
     35:                 }            
    
     36:             }
    
     37:             cout<<endl;
    
     38:         }
    
     39:     }
    
     40:     return 0;
    
     41: }
  • 相关阅读:
    Magento 安装时文件权限 设置
    进度十(10.28)
    进度九(10.27)
    进度八(10.26)
    进度六(10.24)
    进度五(10.23)
    进度四(10.22)
    进度三(10.21)
    进度二(10.20)
    进度一(10.19)
  • 原文地址:https://www.cnblogs.com/malloc/p/1673298.html
Copyright © 2011-2022 走看看