codejam题目_嵌套深度-Google CodeJam 2020资格回合问题解决方案

codejam题目

Problem statement:

问题陈述:

Given a string of digits S, insert a minimum number of opening and closing parentheses into it such that the resulting string is balanced and each digit d is inside exactly d pairs of matching parentheses.

给定一串数字S ,在其中插入最小数量的开括号和闭括号,以使结果字符串保持平衡,并且每个数字d都在d对匹配括号内。

Let the nesting of two parentheses within a string be the substring that occurs strictly between them. An opening parenthesis and a closing parenthesis that is further to its right are said to match if their nesting is empty, or if every parenthesis in their nesting matches with another parenthesis in their nesting. The nesting depth of a position p is the number of pairs of matching parentheses m such that p is included in the nesting of m

将字符串中两个括号的嵌套作为严格位于它们之间的子字符串。 如果嵌套嵌套为空,或者嵌套中的每个括号都与嵌套中的另一个括号匹配,则将右括号和右括号称为匹配。 位置p的嵌套深度是匹配括号m的对数,因此p包含在m的嵌套中。

For example, in the following strings, all digits match their nesting depth: 0((2)1), (((3))1(2)), ((((4)))), ((2))((2))(1). The first three strings have minimum length among those that have the same digits in the same order, but the last one does not since ((22)1) also has the digits 221 and is shorter.

例如,在以下字符串中,所有数字均与其嵌套深度匹配: 0((2)1),(((3))1(2)),(((((4)))),((2)) ((2))(1) 。 前三个字符串在具有相同顺序的相同数字的字符串中具有最小长度,但最后一个字符串没有,因为((22)1)也具有数字221并且较短。

Given a string of digits S, find another string S', comprised of parentheses and digits, such that,

给定一串数字S,找到另一个由括号和数字组成的字符串S',这样,

  • all parentheses in S' match some other parenthesis,

    S'中的所有括号都与其他括号匹配,

  • removing any and all parentheses from S' results in S,

    从S'中删除所有括号,结果为S,

  • each digit in S' is equal to its nesting depth, and

    S'中的每个数字等于其嵌套深度,并且

  • S' is of minimum length.

    S'为最小长度。

Input:

输入:

The first line of the input gives the number of test cases, T. T lines follow. Each line represents a test case and contains only the string S.

输入的第一行给出测试用例的数量T。 T线跟随。 每行代表一个测试用例,仅包含字符串S。

Output:

输出:

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the string S' defined above.

对于每个测试用例,输出包含Case# x:y的一行,其中x是测试用例编号(从1开始), y是上面定义的字符串S'

Constraints:

限制条件:

1 ≤ T ≤ 100.
1 ≤ length of S ≤ 100.
Each character in S is a decimal digit between 0 and 9, inclusive.

Example:

例:

Input:
4
0000
101
111000
1

Output:
Case #1: 0000
Case #2: (1)0(1)
Case #3: (111)000
Case #4: (1)

Explanation:

说明:

The strings ()0000(), (1)0(((()))1) and (1)(11)000 are not valid solutions to Sample Cases #1, #2 and #3, respectively, only because they are not of minimum length. In addition, 1)( and )(1 are not valid solutions to Sample Case #4 because they contain unmatched parentheses and the nesting depth is 0 at the position where there is a 1.

字符串()0000(),(1)0((((()))1)和(1)(11)000分别不是示例案例#1,#2和#3的有效解决方案没有最小长度。 此外,1)(和)(1)不是样本案例4的有效解决方案,因为它们包含不匹配的括号,并且在存在1的位置处的嵌套深度为0。

You can create sample inputs that are valid only for Test Set 2 by removing the parentheses from the example strings mentioned in the problem statement.

通过从问题陈述中提到的示例字符串中删除括号,可以创建仅对测试集2有效的样本输入。

Source: Qualification Round 2020 - Code Jam 2020 - Nesting Depth

资料来源: 2020年资格赛-2020年《果酱大战》-嵌套深度

Solution

Few points to note:

需要注意的几点:

  1. Number of the opening bracket for digit I is i and number of opening brackets will be counted from the beginning and each closing bracket cancels out each opening bracket

    对于数字开口托架 i和开口括号的数的数量将从开始进行计数,并且每个闭合托架抵消每个开口托架

    For example:

    例如:

    ((2(3))) is valid and as 3 have three opening brackets before it and there's no closing bracket to cancel.

    (((2(3))))是有效的,因为3之前有三个左括号,并且没有可取消的右括号。

  2. What if a digit is less than the previous digit? We can balance by adding closing brackets as necessary to cancel the extra opening brackets out

    如果一个数字小于前一个数字怎么办? 我们可以通过在必要时添加方括号来抵消多余的方括号来平衡

    Continuing the above example:

    继续上面的示例:

    For 231

    为231

    We place 1 after the two closing brackets like below:

    我们将1放在两个闭括号之间,如下所示:

    ((2(3))1

    (((2(3))1

Let the input string be str
Let's take the above example, so str="231"
Now let's give a detailed algo based on the above discussion

让输入字符串为str
让我们以上面的示例为例,因此str =“ 231”
现在,基于以上讨论,给出详细的算法

  1. The result string is initially initialized as empty ""

    结果字符串最初初始化为空的“”

  2. Initially add as many opening brackets as needed as per value of str[0] and then put str[0]

    最初根据str [0]的值添加尽可能多的左括号,然后放入str [0]

    So as per our example

    所以按照我们的例子

    str[0]=2 and thus two opening brackets will be added and then 2

    str [0] = 2 ,因此将添加两个开括号,然后添加2

    So

    所以

    result="((2" as of now

    结果=“(((2”截至目前

  3. For the other indexes i=1 to n-1

    对于其他索引,i = 1n-1

    If(str[i-1]==str[i])
        Simply add the digit
    Else if (str[i-1]<str[i])
        Add (str[i]-str[i-1]) number of opening brackets to balance 
        ( str[i]now have str[i]number of opening brackets before it)
    Else ##Str[i]<str[i-1]
        Add (Str[i-1]-str[i])number of closing brackets to balance 
        (str[i] now have str[i] number of opening brackets before 
        it as extra opening brackets are cancelled
    Finally put str[i]#the digit itself
    
    
  4. Finally put s[n-1] number of closing brackets to end the string,

    最后将s [n-1]个右括号括起来,以该字符串结尾,

    So, str[1]=3 and that's greater than 2. 
    We need (3-2) number of opening brackets
    
    So result ="((2(3"
    
    str[2]=1 and that's less than 3. 
    We need (3-1) number of closing brackets to balance
    
    So result ="((2(3))1"
    
    Finally put 1 closing bracket to end the string
    So final result is "((2(3))1)"
    
    

C++ Implementation:

C ++实现:

#include <bits/stdc++.h>
using namespace std;

int main()
{

    map<char, int> mymap;
    
    mymap['0'] = 0;
    mymap['1'] = 1;
    mymap['2'] = 2;
    mymap['3'] = 3;
    mymap['4'] = 4;
    mymap['5'] = 5;
    mymap['6'] = 6;
    mymap['7'] = 7;
    mymap['8'] = 8;
    mymap['9'] = 9;
    
    int t;
    cin >> t;
    
    for (int test_case = 1; test_case <= t; test_case++) {
        string s;
        cin >> s;
        string result = "";
        int n = s.length();
        for (int i = 0; i < s.length(); i++) {
            if (i == 0) {
                for (int j = 0; j < mymap[s[i]]; j++) {
                    result += string(1, '(');
                }
                result += string(1, s[i]);
            }
            else {
                if (s[i] == s[i - 1]) {
                    result += string(1, s[i]);
                }
                else if (s[i] < s[i - 1]) {
                    int num = mymap[s[i - 1]] - mymap[s[i]];
                    for (int j = 0; j < num; j++)
                        result += string(1, ')');
                    result += string(1, s[i]);
                }
                else {
                    int num = mymap[s[i]] - mymap[s[i - 1]];
                    for (int j = 0; j < num; j++)
                        result += string(1, '(');
                    result += string(1, s[i]);
                }
            }
        }
        for (int j = 0; j < mymap[s[n - 1]]; j++)
            result += string(1, ')');

        cout << "Case #" << test_case << ": " << result << endl;
    }

    return 0;
}

Output:

输出:

3
231
Case #1: ((2(3))1)
321
Case #2: (((3)2)1)
123
Case #3: (1(2(3)))


翻译自: https://www.includehelp.com/icp/nesting-depth-google-codejam-2020-qualification-round-problem-solution.aspx

codejam题目

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值