Baekjun 2776, King of memorization (with.Java)
Baekjun No. 2776, this is an article about King of Memorization (with.Java).
I want to solve the coding test problem, find out how to solve it differently from the retrospective of the problem I solved, and get to know.
Let’s get to the problem first.
Problem
Yeonjong has a great memory.
So I can remember all the integers I saw during the day.
However, Dong-gyu, who cannot believe this, decides to test his memory.
Dong-gyu followed Yeonjong and wrote down all the essence he saw for a day in ‘Paper 1’.
Based on that, Dong-gyu asked Yeon-jong M questions to see if he was a real memorizer.
The question is, “Have you seen the integer X today?”
Yeonjong answered all of them without being blocked, and Dong-gyu wrote down the numbers that Yeonjong claimed to have seen in ‘Handbook 2’.
Back home, Dong-gyu tries to check if the answer is correct, but he is having such a hard time following Yeonjong, so he asked you for help.
To help Dong-gyu, write a program in the order written in ‘Handbook 2’, for each number, 1 if it is in ‘Handbook 1’, and 0 if it is not.
Input
The number of test cases T is shown in the first line.
In the next line, the number N (1 ≤ N ≤ 1,000,000) of integers written in ‘Handbook 1’ is input.
The next line contains N integers written in ‘Handbook 1’.
The next line is given the number M (1 ≤ M ≤ 1,000,000) of integers written in ‘Handbook 2’, and the next line is given M integers written in ‘Handbook 2’.
The range of all integers is int.
Output
In the order of M numbers written in ‘Handbook 2’, 1 is output if it is in ‘Handbook 1’, and 0 is output if it is not.
problem solving
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.BufferedWriter;
import java.io.OutputStreamWriter;
import java.io.IOException;
class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out));
int T = Integer.valueOf(br.readLine());
for(int i = 0; i < T; i++){
int count = 0;
char[] arr = br.readLine().toCharArray();
for(char ch : arr){
if(ch == '('){
count++;
if(count > arr.length){
break;
}
}else{
count--;
if(count < 0){
break;
}
}
}
if(count != 0){
bw.write("NO");
bw.newLine();
}else{
bw.write("YES");
bw.newLine();
}
}
br.close();
bw.flush();
bw.close();
}
}
Solution Description
This code is a program that verifies that each string is correct parentheses given multiple parentheses.
Processing inputs and outputs with BufferedReader and BufferedWriter increases efficiency.
First, read the number T of test cases in the first line.
For each test case, read the parentheses string and convert it into a character array.
After that, the count is increased when the parentheses ‘(‘) are opened while traversing each character, and the count is decreased when the parentheses ‘(‘) are closed.
In this process, if the count becomes negative, it means that there are more parentheses to close than the parentheses to open, so stop repeating.
After the iteration, output “NO” if the count is not 0, and “YES” if it is 0.
This indicates whether each test case is correct parentheses or not.
Finally, after processing all inputs, the BufferedWriter outputs the results, empties and closes the buffer.
This process allows you to efficiently verify that the square bracket string is correct.