Created
October 8, 2018 14:04
-
-
Save completejavascript/a39f06554ffecfc0db6239c7602ed5aa to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Using stack | |
#include <iostream> | |
using namespace std; | |
const int MAX_N = 1000001; | |
char input[MAX_N], output[MAX_N]; | |
// Stack | |
struct Stack { | |
char* data; | |
int length; | |
}; | |
Stack* initStack(int maxLength) { | |
Stack* st = new Stack; | |
st->data = new char[maxLength]; | |
st->length = 0; | |
st->data[st->length] = '\0'; | |
return st; | |
} | |
void destroyStack(Stack *st) { | |
delete[] st->data; | |
delete st; | |
} | |
int getLength(Stack *st) { | |
return st->length; | |
} | |
bool isEmpty(Stack *st) { | |
return st->length == 0; | |
} | |
bool isFull(Stack *st) { | |
return st->length == MAX_N; | |
} | |
void push(Stack *st, char data) { | |
if (!isFull(st) && data != '\0') { | |
st->data[st->length] = data; | |
st->length++; | |
st->data[st->length] = '\0'; | |
} | |
} | |
char pop(Stack *st) { | |
if (!isEmpty(st)) { | |
char data = st->data[st->length - 1]; | |
st->length--; | |
st->data[st->length] = '\0'; | |
return data; | |
} | |
return '\0'; | |
} | |
// End of Stack | |
int getLength(char *input) { | |
int index = 0; | |
while(input[index] != '\0') index++; | |
return index; | |
} | |
void concat(char *output, char *input1, char *input2) { | |
int index1 = 0; | |
while(input1[index1] != '\0') { | |
output[index1++] = input1[index1]; | |
} | |
int index2 = getLength(input2) - 1; | |
int cnt = 0; | |
while(index2 >= 0) { | |
output[index1 + cnt++] = input2[index2--]; | |
} | |
output[index1 + cnt] = '\0'; | |
} | |
void getPassword(char *output, char* input) { | |
Stack* stMain = initStack(MAX_N); | |
Stack* stBuffer = initStack(MAX_N); | |
int index = 0; | |
char current = '\0'; | |
while(true) { | |
current = input[index]; | |
if (current == '\0') break; | |
if (current == '<') { | |
push(stBuffer, pop(stMain)); | |
} else if (current == '>') { | |
push(stMain, pop(stBuffer)); | |
} else if (current == '-') { | |
pop(stMain); | |
} else { | |
push(stMain, current); | |
} | |
index++; | |
} | |
concat(output, stMain->data, stBuffer->data); | |
destroyStack(stMain); | |
destroyStack(stBuffer); | |
} | |
int main() { | |
ios::sync_with_stdio(false); | |
//freopen("input.txt", "r", stdin); | |
int T; | |
cin >> T; | |
for(int tc = 0; tc < T; tc++) { | |
cin >> input; | |
getPassword(output, input); | |
cout << output << endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment