利用 List 模擬實際行為即可得解。
C++(0.170)
/*******************************************************/
/* UVa 11988 Broken Keyboard (a.k.a. Beiju Text) */
/* Author: Maplewing [at] knightzone.studio */
/* Version: 2017/12/21 */
/*******************************************************/
#include <iostream>
#include <cstdio>
#include <list>
using namespace std;
int main(){
string input;
while(getline(cin, input)){
list<char> text;
list<char>::iterator textIterator;
textIterator = text.begin();
for( int i = 0 ; i < input.length() ; ++i ){
switch(input[i]){
case '[':
textIterator = text.begin();
break;
case ']':
textIterator = text.end();
break;
default:
text.insert(textIterator, input[i]);
break;
}
}
for(textIterator = text.begin() ; textIterator != text.end() ; ++textIterator){
printf("%c", *textIterator);
}
printf("\n");
}
return 0;
}