Advertisement
chunkyguy

BalancedSmiley_Fail

Jan 29th, 2013
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.81 KB | None | 0 0
  1. //Balanced Smileys
  2.  
  3. #include <iostream>
  4. #include <string>
  5. #include <cctype>
  6. #include <exception>
  7.  
  8. #define DEBUG
  9.  
  10. using namespace std;
  11.  
  12. bool isValidChar(char c){
  13.     if(c == '\0')
  14.         return false;
  15.    
  16.     char *validStr = "abcdefghijklmnopqrstuvwxyz: ";
  17.     return (strchr(validStr, c) != NULL);
  18. }
  19.  
  20. bool solve(const char *line, unsigned int s, unsigned int e){
  21. #ifdef DEBUG
  22.     cout << "Solve for: ";
  23.     for(unsigned int i = s; i < e; i++){
  24.         cout << line[i] << " ";
  25.     }
  26.     cout << endl;
  27. #endif
  28.    
  29.     unsigned int len = e - s;
  30.     if(len <= 0){
  31. #ifdef DEBUG
  32.         cout << "YES: zero length" << endl;
  33. #endif
  34.         return true;
  35.     }
  36.  
  37.     for (unsigned int indx = s; indx < e; indx++) {
  38.         char c = line[indx];
  39.         if(isValidChar(c)){
  40.             continue;
  41.         }
  42.        
  43.         if(c != '(' && c != ')'){
  44. #ifdef DEBUG
  45.             cout << "NO: some invalid char" << endl;
  46. #endif
  47.             return false;
  48.         }
  49.        
  50.         //case ( or )
  51.        
  52.         //no prob: :) or :(
  53.         if(indx > 0 && line[indx-1] == ':'){
  54.             continue;
  55.         }
  56.        
  57.  
  58.         if(c == ')'){
  59.  
  60. #ifdef DEBUG
  61.             cout << "NO: unbalanced )" << endl;
  62. #endif
  63.             return false;
  64.         }else if(c == '('){
  65.             //find index for corresponding ) backwards
  66.             unsigned int rindx = indx+1;
  67.             unsigned int rindxes[2];    //kinda stack
  68.             int rindxes_ptr = 0;
  69.             for(; rindx < e; rindx++){
  70.                 if(line[rindx] == ')'){
  71.                     if(rindx > 0 && line[rindx-1]!=':'){
  72.                         break;  //index found
  73.                     }
  74.                 }
  75.             }
  76.            
  77.             if(rindx < e){
  78.                 //found. Solve for substr
  79.                 if(solve(line, indx+1, rindx)){
  80.                     indx = rindx;
  81.                 }else{
  82. #ifdef DEBUG
  83.                     cout << "NO: substr not solvable" << endl;
  84. #endif
  85.                     return false;
  86.                 }
  87.             }else{
  88. #ifdef DEBUG
  89.                 cout << "NO: unbalanced (" << endl;
  90. #endif
  91.                 return false;
  92.             }
  93.         }
  94.        
  95.     }
  96. #ifdef DEBUG
  97.     cout << "YES: passed all test" << endl;
  98. #endif
  99.     return true;
  100. }
  101.  
  102. bool solve(string &line){
  103.                                                         cout << "< " <<line << " >      ";
  104.     //empty string
  105.     unsigned int len = line.length();
  106.     if(len == 0){
  107.                                                         cout << "YES: zero length " << endl;
  108.         return true;
  109.     }
  110.  
  111.     for(unsigned int i = 0; i < len; i++){
  112.         if(line[i] == '('){
  113.             bool balanced = false;
  114.             if((i > 0) && (line[i-1] == ':')){
  115.                 balanced = true;                            cout << ":( ";
  116.             }
  117.             for(unsigned int j = i+1; !balanced && j < len; j++){
  118.                 if(line[j] == ')'){
  119.                     balanced = true;                        cout << "(..) ";
  120.                     if(j+1<len){
  121.                         i = j+1;
  122.                     }else{                              cout << "eol ";
  123.                         return true;
  124.                     }
  125.                     break;
  126.                 }else if(!isValidChar(line[j])){            cout << "invalid char = " << line[j] << " ";
  127.                     return false;
  128.                 }
  129.             }
  130.             if(!balanced){                              cout << "unbalanced ( ";
  131.                 return false;
  132.             }
  133.         }else if(line[i] == ')'){
  134.             bool balanced = false;
  135.             if((i > 0) && (line[i-1] == ':')){
  136.                 balanced = true;                            cout << ":) ";
  137.             }
  138.             if(!balanced){                              cout << "unbalanced ) ";
  139.                 return false;
  140.             }
  141.         }else if(!isValidChar(line[i])){                    cout << "invalid char = " << line[i] << " ";
  142.             return false;
  143.         }
  144.     }  
  145.  
  146.     cout << "YES: passed all test";
  147.     return true;
  148.  
  149. }
  150.  
  151. class solution : public exception{
  152. public:
  153.     solution(string &m) : msg(m){
  154.     }
  155.  
  156. private:
  157.     virtual const char *what() const throw(){
  158.         return msg;
  159.     }
  160.     string msg;
  161. };
  162.  
  163. unsigned int solve_rec(string &line, unsigned int indx_start){
  164.     unsigned int indx = indx_start;
  165.    
  166.     for (; indx < line.length(); indx++) {
  167.         char c = line[indx];
  168.         if(!isValidChar(c) && (c != '(' || c != ')')){
  169.             throw solution("Invalid char: NO");
  170.         }
  171.        
  172.         if(c == '('){
  173.             indx = solve_rec(line, indx+1);
  174.         }else if(c == ')'){
  175.             return indx;
  176.         }
  177.     }
  178.  
  179.     return indx;
  180. }
  181.  
  182. int main(int argc, char **argv){
  183.     string line;
  184.     getline(cin, line);
  185.     int t_cases = atoi(line.c_str());
  186.     for(int cno = 0; cno < t_cases; cno++){
  187.         getline(cin, line);
  188.         cout << "Case #" << cno+1 << ": " << (solve(line.c_str(), 0, line.length())?"YES":"NO") << endl;
  189.     }
  190.     return 0;
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement