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 1Output: 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:
需要注意的幾點:
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之前有三個左括號,并且沒有可取消的右括號。
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”
現在,基于以上討論,給出詳細的算法
The result string is initially initialized as empty ""
結果字符串最初初始化為空的“”
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”截至目前
For the other indexes i=1 to n-1
對于其他索引,i = 1到n-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 itselfFinally 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 bracketsSo result ="((2(3"str[2]=1 and that's less than 3. We need (3-1) number of closing brackets to balanceSo 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題目
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的codejam题目_嵌套深度-Google CodeJam 2020资格回合问题解决方案的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 手机子王掩码和网关查找_C程序使用位掩码
- 下一篇: c struct 对齐_C中的struc