题解:AT_abc411_d [ABC411D] Conflict 2
题意
有
1 p:令s_p\leftarrow s_0 。2 p s:令s_p\leftarrow s_p+s ,其中+ 表示字符串拼接。3 p:令s_0\leftarrow s_p 。
执行完所有操作后,求
题解
招笑做法。
显然不能暴力模拟,那么考虑使用链表。具体地,在链表节点上记录前驱
代码
#include <iostream>
#include <string>
using namespace std;
#define lowbit(x) ((x) & -(x))
#define chk_min(x, v) (x) = min((x), (v))
#define chk_max(x, v) (x) = max((x), (v))
typedef long long ll;
typedef pair<int, int> pii;
const int N = 2e5 + 5;
int n, q;
int top;
string stk[N];
int tot;
struct Node {
int prv;
string s;
} nodes[N];
int tail[N];
int main() {
ios::sync_with_stdio(0), cin.tie(0);
cin >> n >> q;
while (q--) {
int op, p; cin >> op >> p;
string str;
if (op == 1) tail[p] = tail[0];
else if (op == 2) {
cin >> str;
nodes[++tot] = {tail[p], str};
tail[p] = tot;
} else tail[0] = tail[p];
}
for (int p = tail[0]; p; p = nodes[p].prv) stk[++top] = nodes[p].s;
for (int i = top; i; --i) cout << stk[i];
return 0;
}