从代码阻塞2020解决“嵌套深度”?

2024-04-19 15:36:03 发布

您现在位置:Python中文网/ 问答频道 /正文

战争法典-2020:筑巢深度

问题是:

你也可以去https://codingcompetitions.withgoogle.com/codejam/round/000000000019fd27/0000000000209a9f问这个问题

tl;dr: 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.

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.

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.

Given a string of digits S, find another string S', comprised of parentheses and digits, such that:
all parentheses in S' match some other parenthesis,
removing any and all parentheses from S' results in S,
each digit in S' is equal to its nesting depth, and
S' is of minimum length.

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.

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.

Limits
Time limit: 20 seconds per test set.
Memory limit: 1GB.
1 ≤ T ≤ 100.
1 ≤ length of S ≤ 100.

Test set 1 (Visible Verdict)
Each character in S is either 0 or 1.

Test set 2 (Visible Verdict)
Each character in S is a decimal digit between 0 and 9, inclusive.

Sample

Input
    
Output
 
4
0000
101
111000
1

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

  
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.

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.

这是我的解决方案:

t = int(input())
elem = []
for x in range(t):
    elem.append(list(map(int, list(input()))))
for x in range(t):
    S = ""
    stock = 0
    for y in range(len(elem[x])):
        try:
            if elem[x][y] > elem[x][y + 1] != 0:
                if elem[x][y - 1] != elem[x][y] :
                    if elem[x][y] > elem[x][y - 1] != 0 and y != 0:
                        for z in range(elem[x][y] - elem[x][y - 1]):
                            S += "("
                    else:
                        for z in range(elem[x][y]-stock):
                            S += "("
                S += str(elem[x][y])
                for z in range(elem[x][y]-elem[x][y+1]):
                    S += ")"
                stock = elem[x][y]-(elem[x][y]-elem[x][y+1])
                # print(stock)
            else:
                if elem[x][y - 1] == elem[x][y] and y != 0:
                    S += str(elem[x][y])
                    # print(elem[x][y-1], elem[x][y])
                elif elem[x][y] > elem[x][y - 1] != 0 and y != 0:
                    for z in range(elem[x][y] - elem[x][y - 1]):
                        S += "("
                    S += str(elem[x][y])
                else:
                    for z in range(elem[x][y]-stock):
                        S += "("
                    S += str(elem[x][y])
                try:
                    if elem[x][y + 1] == elem[x][y] and y != len(elem[x]) - 1:
                        continue
                    elif elem[x][y] < elem[x][y + 1] != 0 and y != len(elem[x]):
                        continue
                    else:
                        if y == len(elem[x]) - 1 :
                            stock = 0
                        else:
                            if elem[x][y+1] == 0:
                                stock = 0
                        for z in range(elem[x][y]-stock):
                            S += ")"
                except:
                    if y == len(elem[x]) - 1:
                        stock = 0
                    else:
                        if elem[x][y + 1] == 0:
                            stock = 0
                    for z in range(elem[x][y]-stock):
                        S += ")"
        except:
            if elem[x][y - 1] == elem[x][y] and y != 0:
                S += str(elem[x][y])
                # print(elem[x][y-1], elem[x][y])
            elif elem[x][y] > elem[x][y - 1] != 0 and y != 0:
                for z in range(elem[x][y] - elem[x][y - 1]):
                    S += "("
                S += str(elem[x][y])
            else:
                for z in range(elem[x][y]-stock):
                    S += "("
                S += str(elem[x][y])
            try:
                if elem[x][y + 1] == elem[x][y] and y != len(elem[x]) - 1:
                    continue
                elif elem[x][y] < elem[x][y + 1] != 0 and y != len(elem[x]):
                    continue
                else:
                    if y == len(elem[x]) - 1:
                        stock = 0
                    else:
                        if elem[x][y + 1] == 0:
                            stock = 0
                    for z in range(elem[x][y]-stock):
                        S += ")"
            except:
                if y == len(elem[x]) - 1:
                    stock = 0
                else:
                    if elem[x][y + 1] == 0:
                        stock = 0
                for z in range(elem[x][y]-stock):
                    S += ")"
    print("Case #"+str(x+1)+": "+S)

有一个小错误导致了错误的答案。如果有人能抽出时间,请通过我的代码来帮助我。这是一项乏味的工作。我已经检查了我的代码6个小时了,但是没有找到答案


Tags: andoftheinforlenifis
1条回答
网友
1楼 · 发布于 2024-04-19 15:36:03

你的代码太枯燥了,不可能把所有的东西都看一遍。请注意,协作并寻求帮助并没有错,但当竞争正在进行时,我无法在此处共享代码

在GCJ给出的示例中,对于321>;(((3))1(2))请注意,两个数字之间的括号数始终是两个数字的差。括号的方向取决于哪个数字更高。如果两个数字相等,则它们之间没有括号。这个解可以在O(N)中完成,其中N是数字的长度

相关问题 更多 >