UVA 11988 - Broken Keyboard (a.k.a. Beiju Text)
题意:给定一个文本,输入'['光标到开头,输入']'光标到结尾,问输入完之后对应的输出是什么
思路:用链表搞,遇到'['就把指针指到链表头,遇到']'就指到链表尾,其他就在指针位置插入即可
代码:
#include <cstdio>
#include <cstring>
#include <list>
#include <iostream>
using namespace std;
const int N = 100005;
char str[N];
list<char> ans;
int main() {
while(gets(str)) {
int len = strlen(str);
ans.clear();
list<char>::iterator it = ans.begin();
for (int i = 0; i < len; i++) {
if (str[i] == '[') it = ans.begin();
else if (str[i] == ']') it = ans.end();
else ans.insert(it, str[i]);
}
for (it = ans.begin(); it != ans.end(); it++)
printf("%c", *it);
printf("\n");
}
return 0;
}