반응형
흠... 왜 이렇게 생각을 못했을까

Balanced Smileys

Your friend John uses a lot of emoticons when you talk to him on Messenger. In addition to being a person who likes to express himself through emoticons, he hates unbalanced parenthesis so much that it makes him go :(

Sometimes he puts emoticons within parentheses, and you find it hard to tell if a parenthesis really is a parenthesis or part of an emoticon.

A message has balanced parentheses if it consists of one of the following:

  • - An empty string ""
  • - One or more of the following characters: 'a' to 'z', ' ' (a space) or ':' (a colon)
  • - An open parenthesis '(', followed by a message with balanced parentheses, followed by a close parenthesis ')'.
  • - A message with balanced parentheses followed by another message with balanced parentheses.
  • - A smiley face ":)" or a frowny face ":("

Write a program that determines if there is a way to interpret his message while leaving the parentheses balanced.

Input

The first line of the input contains a number (1 ≤ T ≤ 50), the number of test cases. 
The following T lines each contain a message of length s that you got from John.

Output

For each of the test cases numbered in order from 1 to T, output "Case #i: " followed by a string stating whether or not it is possible that the message had balanced parentheses. If it is, the string should be "YES", else it should be "NO" (all quotes for clarity only)

Constraints

  • 1 ≤ length of s ≤ 100

Example input
5
:((
i am sick today (:()
(:)
hacker cup: started :):)
)(

Example output
Case #1: NO
Case #2: YES
Case #3: YES
Case #4: YES
Case #5: NO

  1. public class BalancedSmileys {
  2.     public static void main(String[] args) {
  3.         try {
  4.             BufferedReader in = new BufferedReader(new FileReader("src/code/smile.txt"));
  5.             String lineData;
  6.             int lineNumber = 0;
  7.                        
  8.             while ((lineData = in.readLine()) != null) {
  9.                
  10.                 if (lineNumber >= 1){
  11.                     char[] charData = lineData.toCharArray();
  12.                     int parenthesisOpenCount = 0;
  13.                     int parenthesisCloseCount = 0;             
  14.                        
  15.                     String result = "YES";
  16.                    
  17.                     for(int i=0; i<charData.length; i++){
  18.                         char data = charData[i];
  19.                         char preData = ' ';
  20.                        
  21.                         if (i > 0){
  22.                             preData = charData[i-1];
  23.                         }
  24.                                                
  25.                         if (data == '('){                  
  26.                             parenthesisOpenCount++;
  27.                            
  28.                             if (i > 0 && preData != ':'){
  29.                                 parenthesisCloseCount++;
  30.                             }
  31.                         }else if (data == ')'){
  32.                             parenthesisCloseCount = Math.max(0, parenthesisCloseCount-1);
  33.                            
  34.                             if (i > 0 && preData != ':'){
  35.                                 parenthesisOpenCount--;
  36.                                
  37.                                 if (parenthesisOpenCount < 0){
  38.                                     break;
  39.                                 }
  40.                             }
  41.                         }
  42.                     }
  43.                    
  44.                     if (parenthesisOpenCount >= 0 && parenthesisCloseCount == 0 ){
  45.                         result = "YES";
  46.                     }else{
  47.                         result = "NO";
  48.                     }
  49.                    
  50.                    
  51.                     System.out.println("Case #" + lineNumber + ": " + result);
  52.                 }
  53.                 lineNumber++;
  54.             }
  55.             in.close();
  56.         } catch (IOException e) {
  57.             System.err.println(e);
  58.             System.exit(1);
  59.         }
  60.     }
  61. }


728x90
반응형

'Development > Algorithm' 카테고리의 다른 글

에라토스테네스의 체  (0) 2016.12.07
[Try-catch]대문자 출력  (0) 2013.11.04
[Programing Challenges]반전한 수 더하기  (0) 2013.02.12
[Hacker Cup]Find the Min  (0) 2013.02.01
[Programming Challenges] The 3n+1 Problem  (0) 2011.09.01

+ Recent posts