Advertisement
Checosnake

Twitter challenge 2

Jan 19th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.80 KB | None | 0 0
  1. #include <map>
  2. #include <set>
  3. #include <list>
  4. #include <cmath>
  5. #include <ctime>
  6. #include <deque>
  7. #include <queue>
  8. #include <stack>
  9. #include <string>
  10. #include <bitset>
  11. #include <cstdio>
  12. #include <limits>
  13. #include <vector>
  14. #include <climits>
  15. #include <cstring>
  16. #include <cstdlib>
  17. #include <fstream>
  18. #include <numeric>
  19. #include <sstream>
  20. #include <iostream>
  21. #include <algorithm>
  22. #include <unordered_map>
  23.  
  24. using namespace std;
  25.  
  26. deque<char> simplify(deque<char> str)
  27. {
  28.     deque<char> temp;
  29.     int count = 0;
  30.     int skip = 0;
  31.     string input;
  32.     if (str[0] == '(')
  33.     {
  34.         for (int i = 0; i < str.size(); i++)
  35.         {
  36.             input += str[i];
  37.         }
  38.         input.insert(0, "/");
  39.         input += "/";
  40.         for (int i = 0; i < input.length(); i++)
  41.         {
  42.             if (input[i] == '(')
  43.             {
  44.                 for (int j = i + 1; j < input.length(); j++)
  45.                 {
  46.                     if (input[j] == ')' && skip == 0)
  47.                     {
  48.                         input.erase(input.begin() + i);
  49.                         input.insert(i, to_string(count));
  50.                         input.erase(input.begin() + j);
  51.                         input.insert(j, to_string(count));
  52.                         count++;
  53.                         skip = 0;
  54.                         j = input.length();
  55.                     }
  56.                     else if (input[j] == ')' && skip > 0) skip--;
  57.                     else if (input[j] == '(') skip++;
  58.                 }
  59.             }
  60.         }
  61.         count--;
  62.         for (int j = 0; j < input.length(); j++)
  63.         {
  64.             if (isdigit(input[j]))
  65.             {
  66.                 for (int k = j + 1; k < input.length(); k++)
  67.                 {
  68.                     if (input[k] == input[j])
  69.                     {
  70.                         if (input[j - 1] == '/' && input[k + 1] == '/')
  71.                         {
  72.                             input.erase(input.begin() + k);
  73.                             input.erase(input.begin() + j);
  74.                             j--;
  75.                         }
  76.                         else if (isdigit(input[j + 1]) || isdigit(input[k - 1]))
  77.                         {
  78.                         }
  79.                         else if (isalpha(input[j - 1]) || isalpha(input[k - 1]))
  80.                         {
  81.                             input.erase(input.begin() + k);
  82.                             input.erase(input.begin() + j);
  83.                             j--;
  84.                         }
  85.                         break;
  86.                     }
  87.                 }
  88.             }
  89.         }
  90.         input.erase(input.end() - 1);
  91.         input.erase(input.begin());
  92.         for (int i = 0; i < input.length(); i++)
  93.         {
  94.             temp.push_back(input[i]);
  95.         }
  96.         return temp;
  97.     }
  98.     else
  99.     {
  100.         return str;
  101.     }
  102. }
  103.  
  104. int main() {
  105.     //Initialize & Declare Variables As Needed
  106.     string input = " ";
  107.     bool inputover = false;
  108.     deque<char> str;
  109.     deque<char> cmd;
  110.    
  111.     //Cycle until no more input
  112.     while(!inputover)
  113.     {
  114.         //Get input
  115.         getline(cin,input);
  116.        
  117.         //Prepare for next input
  118.         cin.clear();
  119.        
  120.         //Check if end of input
  121.         if(input.empty())
  122.         {
  123.             break;  
  124.         }
  125.    
  126.         //-----------------------------------------------------------
  127.         //Start individual input code below here
  128.         //-----------------------------------------------------------
  129.        
  130.         //Iterate though String
  131.         while(input.length() > 0)
  132.         {
  133.             //Detect slash to indicate instruction
  134.             if(input[0]=='/')
  135.             {
  136.                 input.erase(input.begin());
  137.                 //Iterate through remaining string and split expression from Instruction
  138.                 while(input.length() > 0)
  139.                 {
  140.                     cmd.push_back(input[0]);
  141.                     input.erase(input.begin());
  142.                 }
  143.             }
  144.             else
  145.             {
  146.                 str.push_back(input[0]);
  147.                 input.erase(input.begin());
  148.             }
  149.         }
  150.        
  151.        
  152.         while(!cmd.empty())
  153.         {
  154.            
  155.             if(cmd.front() == 'R')
  156.             {
  157.                 stack<char> temp;
  158.                 while(!str.empty())
  159.                 {
  160.                     if(str.front() == '(')
  161.                     {
  162.                         temp.push(')');
  163.                     }
  164.                     else if(str.front() == ')')
  165.                     {
  166.                         temp.push('(');
  167.                     }
  168.                     else
  169.                     {
  170.                         temp.push(str.front());
  171.                     }
  172.                     str.pop_front();
  173.                 }
  174.                 while(!temp.empty())
  175.                 {
  176.                     str.push_back(temp.top());
  177.                     temp.pop();
  178.                 }
  179.                
  180.             }
  181.             if(cmd.front() =='S')
  182.             {
  183.                 str = simplify(str);        
  184.             }
  185.             cmd.pop_front();
  186.         }
  187.        
  188.         //Display Expression
  189.         //cout << "String" << endl;
  190.         for (int i = 0; i < str.size(); i++)
  191.         {
  192.             cout << str[i];
  193.         }
  194.         cout << endl;
  195.        
  196.         /*
  197.         //Display Instructions
  198.         cout << "Command" << endl;
  199.         for (int i = 0; i < cmd.size(); i++)
  200.         {
  201.             cout << cmd[i];
  202.         }
  203.         cout << endl;
  204.         */
  205.        
  206.        
  207.        
  208.         str.clear();
  209.         cmd.clear();
  210.     }
  211.    
  212.     return 0;
  213. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement