zoukankan      html  css  js  c++  java
  • 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 }
  • 相关阅读:
    ODI ORA-00932: 数据类型不一致: 应为 -, 但却获得 CLOB
    oracle 执行计划简介
    oracle job定时执行存储过程详解
    ODI 目标表主键有序列的同步处理
    ODI 同义词问题
    U盘安装redhat Linux
    ODI ora_01653 表空间无法扩展
    C#使用JSON相关
    常用查询汇总
    EXCEL中汉字转拼音
  • 原文地址:https://www.cnblogs.com/Recoder/p/5468999.html
Copyright © 2011-2022 走看看