matbensch

Eye of Sauron Solution

Sep 13th, 2023
700
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.72 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3.  
  4. using namespace std;
  5.  
  6. int main()
  7. {
  8.     // input the string
  9.     string str;
  10.     cin>>str;
  11.  
  12.     // if we've seen an open parentheses
  13.     bool open_eye = false;
  14.     // if we've seen a close parentheses
  15.     bool close_eye = false;
  16.     // number of bars seen before the eye
  17.     int pre_eye = 0;
  18.     // number of bars seen after the eye
  19.     int post_eye = 0;
  20.  
  21.     // loop through indices
  22.     for(int i=0;i<str.size();i++)
  23.     {
  24.         // if we see a bar
  25.         if(str[i]=='|')
  26.         {
  27.             // if we're looking for a close parentheses, we need to fix it
  28.             if(open_eye)
  29.             {
  30.                 cout << "fix\n";
  31.                 return 0;
  32.             }
  33.             // increment corresponding operator
  34.             if(!close_eye) pre_eye++;
  35.             else post_eye++;
  36.         }
  37.         // if we see close parentheses
  38.         else if(str[i]==')')
  39.         {
  40.             // if we are not looking to close the eye, we need to fix
  41.             if(!open_eye)
  42.             {
  43.                 cout << "fix\n";
  44.                 return 0;
  45.             }
  46.             // close the eye
  47.             open_eye = false;
  48.             close_eye = true;
  49.         }
  50.         // if we see an open parentheses
  51.         else
  52.         {
  53.             // if we already saw the eye, we need to fix
  54.             if(open_eye || close_eye)
  55.             {
  56.                 cout << "fix\n";
  57.                 return 0;
  58.             }
  59.             // open the eye
  60.             open_eye = true;
  61.         }
  62.     }
  63.  
  64.     // if same number of bars before and after eye, it is correct
  65.     if(pre_eye==post_eye) cout << "correct\n";
  66.     // otherwise it needs to be fixed
  67.     else cout << "fix\n";
  68. }
Advertisement
Add Comment
Please, Sign In to add comment