标签:
题意:给你一串合法的括号和当前光标的位置和一些操作,问操作完之后的串是怎么样的
思路:模拟一个双向链表的操作,首先先预处理出配对的括号组,然后模拟即可
#include<bits\stdc++.h> using namespace std; const int maxn = 1e6; struct Node { int l,r; }nodes[maxn]; char s1[maxn],s2[maxn]; int a[maxn],d[maxn]; int main() { int n,m,pos; scanf("%d%d%d",&n,&m,&pos); scanf("%s",s1+1); int len = strlen(s1+1); for (int i = 0;i<=n+1;i++) nodes[i].l=i-1,nodes[i].r=i+1; int now = 0; for (int i = 1;i<=len;i++) { if(s1[i]=='(') a[now++]=i; else d[i]=a[--now],d[a[now]]=i; } scanf("%s",s2+1); // int pos = 0; for (int i = 1;i<=m;i++) { if (s2[i]=='R') pos = nodes[pos].r; else if (s2[i]=='L') pos = nodes[pos].l; else { int l = min(d[pos],pos); int r = max(d[pos],pos); nodes[nodes[l].l].r=nodes[r].r; nodes[nodes[r].r].l=nodes[l].l; pos = nodes[r].r; if (pos==n+1) pos = nodes[n+1].l; if(!pos) pos = nodes[0].r; } } pos = nodes[0].r; while(pos!=n+1) { printf("%c",s1[pos]); pos = nodes[pos].r; } printf("\n"); }
标签:
原文地址:http://blog.csdn.net/qq_21057881/article/details/51348513