Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <map>
- #include <set>
- #include <list>
- #include <cmath>
- #include <ctime>
- #include <deque>
- #include <queue>
- #include <stack>
- #include <string>
- #include <bitset>
- #include <cstdio>
- #include <limits>
- #include <vector>
- #include <climits>
- #include <cstring>
- #include <cstdlib>
- #include <fstream>
- #include <numeric>
- #include <sstream>
- #include <iostream>
- #include <algorithm>
- #include <unordered_map>
- using namespace std;
- deque<char> simplify(deque<char> str)
- {
- deque<char> temp;
- int count = 0;
- int skip = 0;
- string input;
- if (str[0] == '(')
- {
- for (int i = 0; i < str.size(); i++)
- {
- input += str[i];
- }
- input.insert(0, "/");
- input += "/";
- for (int i = 0; i < input.length(); i++)
- {
- if (input[i] == '(')
- {
- for (int j = i + 1; j < input.length(); j++)
- {
- if (input[j] == ')' && skip == 0)
- {
- input.erase(input.begin() + i);
- input.insert(i, to_string(count));
- input.erase(input.begin() + j);
- input.insert(j, to_string(count));
- count++;
- skip = 0;
- j = input.length();
- }
- else if (input[j] == ')' && skip > 0) skip--;
- else if (input[j] == '(') skip++;
- }
- }
- }
- count--;
- for (int j = 0; j < input.length(); j++)
- {
- if (isdigit(input[j]))
- {
- for (int k = j + 1; k < input.length(); k++)
- {
- if (input[k] == input[j])
- {
- if (input[j - 1] == '/' && input[k + 1] == '/')
- {
- input.erase(input.begin() + k);
- input.erase(input.begin() + j);
- j--;
- }
- else if (isdigit(input[j + 1]) || isdigit(input[k - 1]))
- {
- }
- else if (isalpha(input[j - 1]) || isalpha(input[k - 1]))
- {
- input.erase(input.begin() + k);
- input.erase(input.begin() + j);
- j--;
- }
- break;
- }
- }
- }
- }
- input.erase(input.end() - 1);
- input.erase(input.begin());
- for (int i = 0; i < input.length(); i++)
- {
- temp.push_back(input[i]);
- }
- return temp;
- }
- else
- {
- return str;
- }
- }
- int main() {
- //Initialize & Declare Variables As Needed
- string input = " ";
- bool inputover = false;
- deque<char> str;
- deque<char> cmd;
- //Cycle until no more input
- while(!inputover)
- {
- //Get input
- getline(cin,input);
- //Prepare for next input
- cin.clear();
- //Check if end of input
- if(input.empty())
- {
- break;
- }
- //-----------------------------------------------------------
- //Start individual input code below here
- //-----------------------------------------------------------
- //Iterate though String
- while(input.length() > 0)
- {
- //Detect slash to indicate instruction
- if(input[0]=='/')
- {
- input.erase(input.begin());
- //Iterate through remaining string and split expression from Instruction
- while(input.length() > 0)
- {
- cmd.push_back(input[0]);
- input.erase(input.begin());
- }
- }
- else
- {
- str.push_back(input[0]);
- input.erase(input.begin());
- }
- }
- while(!cmd.empty())
- {
- if(cmd.front() == 'R')
- {
- stack<char> temp;
- while(!str.empty())
- {
- if(str.front() == '(')
- {
- temp.push(')');
- }
- else if(str.front() == ')')
- {
- temp.push('(');
- }
- else
- {
- temp.push(str.front());
- }
- str.pop_front();
- }
- while(!temp.empty())
- {
- str.push_back(temp.top());
- temp.pop();
- }
- }
- if(cmd.front() =='S')
- {
- str = simplify(str);
- }
- cmd.pop_front();
- }
- //Display Expression
- //cout << "String" << endl;
- for (int i = 0; i < str.size(); i++)
- {
- cout << str[i];
- }
- cout << endl;
- /*
- //Display Instructions
- cout << "Command" << endl;
- for (int i = 0; i < cmd.size(); i++)
- {
- cout << cmd[i];
- }
- cout << endl;
- */
- str.clear();
- cmd.clear();
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement