日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時(shí)間:2025/3/11 编程问答 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 codejam题目_嵌套深度-Google CodeJam 2020资格回合问题解决方案 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

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.

給定一串?dāng)?shù)字S ,在其中插入最小數(shù)量的開括號(hào)和閉括號(hào),以使結(jié)果字符串保持平衡,并且每個(gè)數(shù)字d都在d對(duì)匹配括號(hào)內(nèi)。

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.?

將字符串中兩個(gè)括號(hào)的嵌套作為嚴(yán)格位于它們之間的子字符串。 如果嵌套嵌套為空,或者嵌套中的每個(gè)括號(hào)都與嵌套中的另一個(gè)括號(hào)匹配,則將右括號(hào)和右括號(hào)稱為匹配。 位置p的嵌套深度是匹配括號(hào)m的對(duì)數(shù),因此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.

例如,在以下字符串中,所有數(shù)字均與其嵌套深度匹配: 0((2)1),(((3))1(2)),(((((4)))),((2)) ((2))(1) 。 前三個(gè)字符串在具有相同順序的相同數(shù)字的字符串中具有最小長(zhǎng)度,但最后一個(gè)字符串沒有,因?yàn)?(22)1)也具有數(shù)字221并且較短。

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

給定一串?dāng)?shù)字S,找到另一個(gè)由括號(hào)和數(shù)字組成的字符串S',這樣,

  • all parentheses in S' match some other parenthesis,

    S'中的所有括號(hào)都與其他括號(hào)匹配,

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

    從S'中刪除所有括號(hào),結(jié)果為S,

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

    S'中的每個(gè)數(shù)字等于其嵌套深度,并且

  • S' is of minimum length.

    S'為最小長(zhǎng)度。

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.

輸入的第一行給出測(cè)試用例的數(shù)量T。 T線跟隨。 每行代表一個(gè)測(cè)試用例,僅包含字符串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.

對(duì)于每個(gè)測(cè)試用例,輸出包含Case# x:y的一行,其中x是測(cè)試用例編號(hào)(從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:

說(shuō)明:

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的有效解決方案沒有最小長(zhǎng)度。 此外,1)(和)(1)不是樣本案例4的有效解決方案,因?yàn)樗鼈儼黄ヅ涞睦ㄌ?hào),并且在存在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.

通過(guò)從問題陳述中提到的示例字符串中刪除括號(hào),可以創(chuàng)建僅對(duì)測(cè)試集2有效的樣本輸入。

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

資料來(lái)源: 2020年資格賽-2020年《果醬大戰(zhàn)》-嵌套深度

Solution

Few points to note:

需要注意的幾點(diǎn):

  • 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

    對(duì)于數(shù)字開口托架 我是i和開口括號(hào)的數(shù)的數(shù)量將從開始進(jìn)行計(jì)數(shù),并且每個(gè)閉合托架抵消每個(gè)開口托架

    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))))是有效的,因?yàn)?之前有三個(gè)左括號(hào),并且沒有可取消的右括號(hào)。

  • 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

    如果一個(gè)數(shù)字小于前一個(gè)數(shù)字怎么辦? 我們可以通過(guò)在必要時(shí)添加方括號(hào)來(lái)抵消多余的方括號(hào)來(lái)平衡

    Continuing the above example:

    繼續(xù)上面的示例:

    For 231

    為231

    We place 1 after the two closing brackets like below:

    我們將1放在兩個(gè)閉括號(hào)之間,如下所示:

    ((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”
    現(xiàn)在,基于以上討論,給出詳細(xì)的算法

  • The result string is initially initialized as empty ""

    結(jié)果字符串最初初始化為空的“”

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

    最初根據(jù)str [0]的值添加盡可能多的左括號(hào),然后放入str [0]

    So as per our example

    所以按照我們的例子

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

    str [0] = 2 ,因此將添加兩個(gè)開括號(hào),然后添加2

    So

    所以

    result="((2" as of now

    結(jié)果=“(((2”截至目前

  • For the other indexes i=1 to n-1

    對(duì)于其他索引,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 itself
  • Finally put s[n-1] number of closing brackets to end the string,

    最后將s [n-1]個(gè)右括號(hào)括起來(lái),以該字符串結(jié)尾,

    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 ++實(shí)現(xiàn):

    #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題目

    創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

    總結(jié)

    以上是生活随笔為你收集整理的codejam题目_嵌套深度-Google CodeJam 2020资格回合问题解决方案的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。