Skip to content

Instantly share code, notes, and snippets.

@aliciawyy
Created July 28, 2019 19:07
Show Gist options
  • Save aliciawyy/78378b60c6c6f4b58a6f052766f17972 to your computer and use it in GitHub Desktop.
Save aliciawyy/78378b60c6c6f4b58a6f052766f17972 to your computer and use it in GitHub Desktop.
UVa 11988 Broken Keyboard
#include <bits/stdc++.h>
using namespace std;
/* Solve the problem with double-linked list
* We just need to record and update the next position to insert
* the character.
*/
int main() {
string line;
while (getline(cin, line)) {
list<char> ans;
auto next_pos = ans.end();
for (auto ch : line) {
if (ch == '[') {
next_pos = ans.begin();
} else if (ch == ']') {
next_pos = ans.end();
} else {
next_pos = next(ans.insert(next_pos, ch));
}
}
cout << string(ans.begin(), ans.end()) << endl;
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment