描述现在给出一段全是字符序列的文章。光标可以被安置在文章的内部任何两个连续的字符中间或在开头第一个字符的左边或在结尾最后一个字符的右边。要求在文章中执行一系列的操作。可能的操作如下:L:将光标左移一个字符如果光标在开头则不用做任何操作。D:将光标右移一个字符如果光标在结尾则不用做任何操作。B:删除光标左边的一个字符如果光标在开头则不用做任何操作。P:在光标的右边插入字符$可以是英文字母表中的任何一
#include
using namespace std;
int main() { string article; cin >> article;
int N;
cin >> N;
stack<char> left;
stack<char> right;
for (char c : article) {
right.push(c);
}
while (N--) {
char operation;
cin >> operation;
if (operation == 'L') {
if (!left.empty()) {
right.push(left.top());
left.pop();
}
} else if (operation == 'D') {
if (!right.empty()) {
left.push(right.top());
right.pop();
}
} else if (operation == 'B') {
if (!left.empty()) {
left.pop();
}
} else if (operation == 'P') {
char c;
cin >> c;
left.push(c);
}
}
while (!left.empty()) {
right.push(left.top());
left.pop();
}
string result;
while (!right.empty()) {
result += right.top();
right.pop();
}
cout << result << endl;
return 0;
原文地址: http://www.cveoy.top/t/topic/iPhP 著作权归作者所有。请勿转载和采集!