Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor (链表)

题目链接:http://codeforces.com/contest/670/problem/E

给你n长度的括号字符,m个操作,光标初始位置是p,'D'操作表示删除当前光标所在的字符对应的括号字符以内的所有字符(比如'(()())'),'R'操作表示右移光标,'L'操作表示左移光标。删除操作后光标向右移,要是再向右移没有字符的话,那就停在最后的字符上了。问你最后的括号字符是怎么样的。

这题用链表写简单多了,直接模拟操作就行了,我用stl里的list。

 1 #include <bits/stdc++.h>
 2 using namespace std;
 3 char str[500010] , op[500010];
 4 int main()
 5 {
 6     ios::sync_with_stdio(false);
 7     int n , m , p , r = 0 , l = 0;
 8     list <char> L;
 9     cin >> n >> m >> p >> str >> op;
10     for(int i = 0 ; i < n ; ++i)
11         L.push_back(str[i]);
12     auto index = L.begin();
13     while(--p) {
14         index++;
15     }
16     for(int i = 0 ; i < m ; i++) {
17         if(op[i] == 'L') {
18             if(index != L.begin()) {
19                 index--;
20             }
21         }
22         else if(op[i] == 'R') {
23             if(++index == L.end()) {
24                 index--;
25             }
26         }
27         else {
28             l = r = 0;
29             if(*index == ')')
30                 r++;
31             else
32                 l++;
33             if(l < r) {
34                 L.erase(index--);
35                 while(r != l) {
36                     if(*index == ')')
37                         r++;
38                     else
39                         l++;
40                     L.erase(index--);
41                 }
42                 if(++index == L.end()) {
43                     index--;
44                 }
45             }
46             else {
47                 L.erase(index++);
48                 while(r != l) {
49                     if(*index == ')')
50                         r++;
51                     else
52                         l++;
53                     L.erase(index++);
54                 }
55                 if(index == L.end()) {
56                     index--;
57                 }
58             }
59         }
60     }
61     index = L.begin();
62     for( ; index != L.end() ; ++index) {
63         cout << *index;
64     }
65     cout << endl;
66 }

 

posted @ 2016-05-07 19:39  Recoder  阅读(280)  评论(0编辑  收藏  举报