Sunday, 5 April 2020

Google CodeJam 2020 Qualified Round Question 2

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.

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.

Solution:

import java.util.*;

class Solution {
    public static void main (String[] args) {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        int l=1;
        while(l<=t)
        {
            String s=sc.next();
          String temp="";
          int cur=0;
          for(int i=0;i<s.length();i++)
          {
          
              int c=Integer.parseInt(Character.toString(s.charAt(i)));
              while(cur<c)
              {
              temp+="(";cur++;
              }
              while(cur>c)
              {
              temp+=")";cur--;
              }
              temp+=Character.toString(s.charAt(i));
          }
          while(cur>0)
          {
          temp+=")";cur--;
          }
            System.out.println("Case #"+l+": "+temp);
            l++;
        }
    }
}

Google CodeJam 2020 Qualified Round Question 1

Problem

Vestigium means "trace" in Latin. In this problem we work with Latin squares and matrix traces.
The trace of a square matrix is the sum of the values on the main diagonal (which runs from the upper left to the lower right).
An N-by-N square matrix is a Latin square if each cell contains one of N different values, and no value is repeated within a row or a column. In this problem, we will deal only with "natural Latin squares" in which the N values are the integers between 1 and N.
Given a matrix that contains only integers between 1 and N, we want to compute its trace and check whether it is a natural Latin square. To give some additional information, instead of simply telling us whether the matrix is a natural Latin square or not, please compute the number of rows and the number of columns that contain repeated values.

Input

The first line of the input gives the number of test cases, T. T test cases follow. Each starts with a line containing a single integer N: the size of the matrix to explore. Then, N lines follow. The i-th of these lines contains N integers Mi,1, Mi,2 ..., Mi,N. Mi,j is the integer in the i-th row and j-th column of the matrix.

Output

For each test case, output one line containing Case #x: k r c, where x is the test case number (starting from 1), k is the trace of the matrix, r is the number of rows of the matrix that contain repeated elements, and c is the number of columns of the matrix that contain repeated elements.

Limits

Test set 1 (Visible Verdict)

Time limit: 20 seconds per test set.
Memory limit: 1GB.
1 ≤ T ≤ 100.
2 ≤ N ≤ 100.
1 ≤ Mi,j  N, for all i, j.

Sample


Input
 

Output
 
3
4
1 2 3 4
2 1 4 3
3 4 1 2
4 3 2 1
4
2 2 2 2
2 3 2 3
2 2 2 3
2 2 2 2
3
2 1 3
1 3 2
1 2 3

  
Case #1: 4 0 0
Case #2: 9 4 4
Case #3: 8 0 2

  
In Sample Case #1, the input is a natural Latin square, which means no row or column has repeated elements. All four values in the main diagonal are 1, and so the trace (their sum) is 4.
In Sample Case #2, all rows and columns have repeated elements. Notice that each row or column with repeated elements is counted only once regardless of the number of elements that are repeated or how often they are repeated within the row or column. In addition, notice that some integers in the range 1 through N may be absent from the input.
In Sample Case #3, the leftmost and rightmost columns have repeated elements.

Solution :- 

import java.util.*;

class Solution {
    public static void main (String[] args) {
        Scanner sc=new Scanner(System.in);
        int t=sc.nextInt();
        int l=1;
        while(l<=t)
        {
            int n=sc.nextInt();
            int a[][]=new int[n][n];
            int r=0,c=0,s=0,d=0;
            int res=(n*(n+1))/2;
            Set<Integer>hs;
            for(int i=0;i<n;i++)
            {
                hs=new HashSet<Integer>();
                for(int j=0;j<n;j++)
                {
                    a[i][j]=sc.nextInt();
                    hs.add(a[i][j]);
                    if(i==j)
                     d+=a[i][j];
                }
                if(hs.size()!=n)
                 r++;
            }
            for(int i=0;i<n;i++)
            {
              hs=new HashSet<Integer>();
                for(int j=0;j<n;j++)
                {
                   hs.add(a[j][i]);
                }
               if(hs.size()!=n)
                 c++;
            }
            System.out.println("Case #"+l+": "+d+" "+r+" "+c);
            l++;
        }
    }
}