Nesting Depth for CodeJam- Have solved prompt, but need to concatenate Strings- program returning "Case # :" in output

huangapple 未分类评论43阅读模式
英文:

Nesting Depth for CodeJam- Have solved prompt, but need to concatenate Strings- program returning "Case # :" in output

问题

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;

class Solution
{
    public static void main(String[] args) throws NumberFormatException, IOException
    {

        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int t = Integer.parseInt(br.readLine());
        for (int tt = 0; tt < t; tt++)
        {
            String s = br.readLine();
            int n = s.length();
            int brac=0;
            StringBuilder sb = new StringBuilder();
            for(int i=0;i<n;i++)
            {
                int val = s.charAt(i)-48;
                if(val==brac)
                {
                    sb.append(s.charAt(i));
                }
                else if(val>brac)
                {
                    int diff = val-brac;
                    sb.append(generateOpenBraces(diff)).append(s.charAt(i));
                    brac = brac+diff;
                }
                else
                {
                    int diff = brac-val;
                    sb.append(generateCloseBraces(diff)).append(s.charAt(i));
                    brac=brac-diff;
                }
            }
            if(brac>0) 
            {
                sb.append(generateCloseBraces(brac));
            }
            System.out.println(sb.toString());

        }
        System.exit(0);
    }

    public static String generateOpenBraces(int n)
    {
         return String.join("", Collections.nCopies(n, "("));
    }
    public static String generateCloseBraces(int n)
    {
         return String.join("", Collections.nCopies(n, ")"));
    }
}
英文:

I've not been able to add/ I'm not sure where to add the strings which say "Case #: " etc. for the Nesting Depth prompt. I've been trying to add this in, but wherever I do I create errors in the code. Please help if you can. xx

Here is the prompt:
Problem
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.

These are the inputs and what should be the resulting output- My code(below) is missing the Case #1: Case #2: etc portion

Input
4
0000
101
111000
1

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

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Collections;

class Solution
{
    public static void main(String[] args) throws NumberFormatException, IOException
    {




        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        int t = Integer.parseInt(br.readLine());
        for (int tt = 0; tt &lt; t; tt++)
        {
            String s = br.readLine();
            int n = s.length();
            int brac=0;
            StringBuilder sb = new StringBuilder();
            for(int i=0;i&lt;n;i++)
            {
                int val = s.charAt(i)-48;
                if(val==brac)
                {
                    sb.append(s.charAt(i));
                }
                else if(val&gt;brac)
                {
                    int diff = val-brac;
                    sb.append(generateOpenBraces(diff)).append(s.charAt(i));
                    brac = brac+diff;
                }
                else
                {
                    int diff = brac-val;
                    sb.append(generateCloseBraces(diff)).append(s.charAt(i));
                    brac=brac-diff;
                }
            }
            if(brac&gt;0) 
            {
                sb.append(generateCloseBraces(brac));
            }
            System.out.println(sb.toString());

        }
        System.exit(0);
    }

    public static String generateOpenBraces(int n)
    {
         return String.join(&quot;&quot;, Collections.nCopies(n, &quot;(&quot;));
    }
    public static String generateCloseBraces(int n)
    {
         return String.join(&quot;&quot;, Collections.nCopies(n, &quot;)&quot;));
    }


}
























</details>


# 答案1
**得分**: 0

只需将`System.out.println(sb.toString())`更改为`System.out.println("Case #" + tt + ": " + sb.toString());`。

<details>
<summary>英文:</summary>

Just change `System.out.println(sb.toString())` to `System.out.println(&quot;Case #&quot; + tt + &quot;: &quot;  + sb.toString());`

</details>



huangapple
  • 本文由 发表于 2020年4月5日 07:23:09
  • 转载请务必保留本文链接:https://java.coder-hub.com/61036083.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定