ผลต่างระหว่างรุ่นของ "01204212/paren"
ไปยังการนำทาง
ไปยังการค้นหา
Jittat (คุย | มีส่วนร่วม) (หน้าที่ถูกสร้างด้วย ':from 01204212 You have a string consisting of parentheses ( ), curly braces { }, and brackets [ ]. You want to check if they are...') |
Jittat (คุย | มีส่วนร่วม) |
||
| (ไม่แสดง 3 รุ่นระหว่างกลางโดยผู้ใช้คนเดียวกัน) | |||
| แถว 15: | แถว 15: | ||
You want to write a program that performs the balance checking. | You want to write a program that performs the balance checking. | ||
| + | |||
| + | == Input/Output == | ||
| + | |||
| + | '''Input''' | ||
| + | |||
| + | * The first line: an integer '''T''', the number of test cases. | ||
| + | * The next '''T''' lines: each line contains a string representing each test case. | ||
| + | |||
| + | '''Output''' | ||
| + | |||
| + | For each test case, output either <tt>yes</tt> if the string is balanced, and <tt>no</tt> if the string is not balanced. | ||
| + | |||
| + | == Example == | ||
| + | |||
| + | '''Input''' | ||
| + | |||
| + | <pre> | ||
| + | 8 | ||
| + | ()()(()) | ||
| + | (((( | ||
| + | [[](){{}}] | ||
| + | [][][]()(){} | ||
| + | [[{]}] | ||
| + | []] | ||
| + | (())[[]]{{}}({[]}) | ||
| + | []][ | ||
| + | </pre> | ||
| + | |||
| + | '''Output''' | ||
| + | |||
| + | <pre> | ||
| + | yes | ||
| + | no | ||
| + | yes | ||
| + | yes | ||
| + | no | ||
| + | no | ||
| + | yes | ||
| + | no | ||
| + | </pre> | ||
| + | |||
| + | == Test data == | ||
| + | |||
| + | == Code == | ||
| + | <syntaxhighlight lang="java"> | ||
| + | import java.io.BufferedReader; | ||
| + | import java.io.InputStreamReader; | ||
| + | |||
| + | public class Main { | ||
| + | |||
| + | public static void main(String[] args) throws Exception { | ||
| + | Main m = new Main(); | ||
| + | m.process(); | ||
| + | } | ||
| + | |||
| + | private void process() throws Exception { | ||
| + | BufferedReader reader = new BufferedReader( | ||
| + | new InputStreamReader(System.in) ); | ||
| + | |||
| + | int t = Integer.parseInt(reader.readLine()); | ||
| + | |||
| + | for(int i=0; i<t; i++) { | ||
| + | String st = reader.readLine(); | ||
| + | |||
| + | if(check(st)) { | ||
| + | System.out.println("yes"); | ||
| + | } else { | ||
| + | System.out.println("no"); | ||
| + | } | ||
| + | } | ||
| + | } | ||
| + | |||
| + | private boolean check(String st) { | ||
| + | // ... your code here | ||
| + | } | ||
| + | } | ||
| + | </syntaxhighlight> | ||
รุ่นแก้ไขปัจจุบันเมื่อ 04:30, 8 กันยายน 2559
- from 01204212
You have a string consisting of parentheses ( ), curly braces { }, and brackets [ ]. You want to check if they are properly balanced. The following are examples of balanced strings:
(()())(())((())())
[()[]]
{[]}{[][](()[])}
These are unbalanced strings:
)(
[[))
[{]}
{{{{}}}}}
You want to write a program that performs the balance checking.
เนื้อหา
Input/Output
Input
- The first line: an integer T, the number of test cases.
- The next T lines: each line contains a string representing each test case.
Output
For each test case, output either yes if the string is balanced, and no if the string is not balanced.
Example
Input
8
()()(())
((((
[[](){{}}]
[][][]()(){}
[[{]}]
[]]
(())[[]]{{}}({[]})
[]][
Output
yes no yes yes no no yes no
Test data
Code
import java.io.BufferedReader;
import java.io.InputStreamReader;
public class Main {
public static void main(String[] args) throws Exception {
Main m = new Main();
m.process();
}
private void process() throws Exception {
BufferedReader reader = new BufferedReader(
new InputStreamReader(System.in) );
int t = Integer.parseInt(reader.readLine());
for(int i=0; i<t; i++) {
String st = reader.readLine();
if(check(st)) {
System.out.println("yes");
} else {
System.out.println("no");
}
}
}
private boolean check(String st) {
// ... your code here
}
}