E题思路
作者:
bearono
,
2025-03-08 21:06:47
· 辽宁
,
所有人可见
,
阅读 6
模拟
#include<bits/stdc++.h>
using namespace std;
int main()
{
string i;
getline(cin, i);
string o;
getline(cin, o);
string c = i;
int p = c.length();//光标一开始在最后
vector<string> v;
stringstream s(o);//使用字符串流stringstream 将字符串分割
string t;
while (s >> t)
{
v.push_back(t);
}
/*
stringstream s(o);
while(s>>t)v.push_back(t);
*/
//size_t 无符号整数
for (size_t x = 0; x < v.size(); ) {
string u = v[x];//当前指令
if (u == "LEFT")
{
if (p > 0) //判断不是在最前
{
p--;
}
x++;//调出下一个指令
}
else if (u == "RIGHT")
{
if (p < c.length())//确定不是最后位置
{
p++;
}
x++;
}
else if (u == "CTRL") //注意这里靠前缀确定好 两个字符串
{
if (x + 1 < v.size()) //至少两个字符串
{
string n = v[x + 1];//这样才能取出后一个
if (n == "S")
{
cout << c << endl;
x += 2;//两个字符串
}
else if (n[0] == 'D' && n.size() > 1)
{
string d = n.substr(1);//取出数字部分
int y = stoi(d);//化为整数
if (p > 0)
{
char z = c[p - 1];//调出光标前面的字母
c.insert(p, y, z);//在p位置插入y个z
p += y;//然后光标移动到插入后位置
}
x += 2;//移动两个字符串
}
else
{
x++;//无效命令也考虑
}
}
else
{
x++;//无效命令也考虑
}
}
else
{
x++;//无效命令也考虑
}
}
cout << c << endl;
return 0;
}