fork(3) download
  1. import java.util.Scanner;
  2. import java.util.Stack;
  3.  
  4. public class Main {
  5.  
  6. public static void main(String[] args) {
  7. // TODO Auto-generated method stub
  8.  
  9. Scanner scan = new Scanner(System.in);
  10. String temp = scan.nextLine();
  11.  
  12. Stack bracket = new Stack<Character>();
  13.  
  14. int flag;
  15. int total=0; int size =0; char tmp; char previous=1;
  16. int n[] = new int[(int)temp.length()+1];
  17.  
  18. for(int i=0; i<temp.length(); i++) {
  19.  
  20. char c = temp.charAt(i);//현재
  21.  
  22. if((c == ')' || c == ']') && (previous ==')'|| previous == ']'))
  23. flag = 1;
  24. else flag = 0;
  25.  
  26. previous = c;//과거꺼
  27.  
  28. if ( c == '(' || c == '[') { //여는 괄호 일 떄,
  29. bracket.push(c);
  30.  
  31. if( c == '(')
  32. n[size++] = 2;
  33. else n[size++] = 3; // {
  34. }
  35.  
  36.  
  37. else if ( c ==')' || c ==']') { // 닫는 괄호 일 때,
  38. if(bracket.isEmpty()) break;
  39.  
  40. tmp = (char) bracket.pop(); //스택에 남아 있던게
  41.  
  42. if( tmp == '('&& c ==')') // ), }일 때 따로해서 에러 잡기
  43. {
  44. int a = 1;
  45. for(int j =0; j<size; j++)
  46. a *= n[j];
  47. if(flag!=1) total+=a;
  48. size--;
  49. }
  50.  
  51. else if( tmp == '['&& c ==']')
  52. {
  53. int a=1;
  54. for(int j =0; j<size; j++)
  55. a *= n[j];
  56. if(flag!=1) total+=a;
  57. size--;
  58. }
  59.  
  60. else {break;}
  61.  
  62. }
  63.  
  64. }
  65.  
  66. if(bracket.isEmpty()) System.out.println(total);
  67. else System.out.println("0");
  68.  
  69.  
  70. }
  71.  
  72. }
Success #stdin #stdout 0.07s 2184192KB
stdin
())([]
stdout
2