思路
奇奇怪怪的指针
看到题先想到了——维护两个指针数组 head[ ] next[ ], 分别表示从哪来到哪去
然后就每次修改数组,最后找到开头的字符 一直跳到 结尾
注意 数组下标不为负的特判 以及初始化
代码
#include<iostream> #include <cstring> using namespace std; string s, c; const int N = 1e6 + 5; int __head[N], __next[N]; int x; int main() { cin >> s>> c; memset(__head, -1, sizeof(__head)); memset(__next, -1, sizeof(__next)); int n = s.length(); for(int i = 0; i < n - 1; i++) { if(c[i] == 'L') { if(__head[i] != -1) __next[__head[i]] = i + 1; __head[i + 1] = __head[i]; __head[i] = i + 1; __next[i + 1] = i; } else { __head[__next[i]] = i + 1; __next[i + 1] = __next[i]; __next[i] = i + 1; __head[i + 1] = i; } } for(int i = 0; i <c.length(); i++) { if(__head[i] == -1){ x = i; break; } } cout << s[x]; while(__next[x] != -1) { x = __next[x]; cout << s[x]; } return 0; }