题解【ABC158D】String Formation
比较基础的套路题。
对于操作 \(1\),将翻转次数标记 \(+1\)。
对于操作 \(2\),设加在开头的字符串为 \(x\),加在结尾的字符串为 \(y\),当前要加的字母为 \(c\):
- 若翻转次数为奇数次,
- 如果在开头加,那么
y = y + c
; - 否则,
x = c + x
。
- 如果在开头加,那么
- 若翻转次数为偶数次,
- 如果在开头加,那么
x = c + x
; - 否则,
y = y + c
。
- 如果在开头加,那么
最后,如果翻转次数为奇数,那么需要将最初的字符串、\(x\) 和 \(y\) 全部翻转一遍,并且交换 \(x\) 和 \(y\)。
#pragma GCC optimize(2)
#include <bits/stdc++.h>
#define DEBUG fprintf(stderr, "Passing [%s] line %d\n", __FUNCTION__, __LINE__)
#define File(x) freopen(x".in","r",stdin); freopen(x".out","w",stdout)
using namespace std;
typedef long long LL;
typedef pair <int, int> PII;
typedef pair <int, PII> PIII;
inline int gi()
{
int f = 1, x = 0; char c = getchar();
while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();}
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return f * x;
}
inline LL gl()
{
LL f = 1, x = 0; char c = getchar();
while (c < '0' || c > '9') {if (c == '-') f = -1; c = getchar();}
while (c >= '0' && c <= '9') x = x * 10 + c - '0', c = getchar();
return f * x;
}
const int INF = 0x3f3f3f3f;
string x, y, s;
struct Node
{
int id, ty;
char c;
} a[200003];
int main()
{
getline(cin, s);
int q = gi();
for (int i = 1; i <= q; i+=1)
{
a[i].id = gi();
if (a[i].id == 2)
{
a[i].ty = gi();
scanf("%c", &a[i].c);
}
}
int tot = 0;
for (int i = 1; i <= q; i+=1)
{
if (a[i].id == 1) ++tot;
else
{
if (tot & 1)
{
if (a[i].ty == 1) y = y + a[i].c;
else x = a[i].c + x;
}
else
{
if (a[i].ty == 1) x = a[i].c + x;
else y = y + a[i].c;
}
}
}
if (tot & 1)
{
reverse(s.begin(), s.end());
reverse(x.begin(), x.end());
reverse(y.begin(), y.end());
swap(x, y);
}
cout << x << s << y << endl;
return 0;
}