破损的键盘

#include<cstdio> #include<cstring> #include<iostream> #include<list> using namespace std; int main(void) { string s; while (cin >> s) { list<char> ch; list<char>::iterator it = ch.begin(); for (auto a : s) { if (a == '[')it = ch.begin(); else if (a == ']')it = ch.end(); else { it =ch.insert(it, a);//insert的这种写法有返回值,需要用迭代器来接受位置否则会发生错误 it++; } } for (it = ch.begin(); it != ch.end(); it++) cout << *it; cout << endl; } return 0; }
#include<cstdio> #include<cstring> using namespace std; const int maxn = 100000 + 5; int last, cur,nexti[maxn]; char s[maxn]; int main(void) { while (scanf("%s", s + 1) == 1) { int n = strlen(s + 1); last = cur = 0; nexti[0] = 0; for (int i = 1; i <= n; i++)//核心代码 { char ch = s[i]; if (ch == '[')cur = 0; else if (ch == ']')cur = last; else { nexti[i] =nexti[cur]; nexti[cur] = i; if (cur == last)last = i; cur = i; } } for (int i = nexti[0]; i != 0; i = nexti[i]) printf("%c", s[i]); printf("\n"); } return 0; }
计算机小白记录学习过程,喜欢就点个推荐和关注吧O(∩_∩)O哈哈~

浙公网安备 33010602011771号