您的位置:首页 > 产品设计 > UI/UE

Codeforces Round #350 (Div. 2) E. Correct Bracket Sequence Editor 模拟

2016-05-30 00:39 399 查看

题目链接:

http://codeforces.com/contest/670/problem/E

题解:

用STL的list和stack模拟的,没想到跑的还挺快。

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<list>
#include<stack>
using namespace std;

const int maxn = 500000 + 10;
const int INF = 2e9;
typedef __int64 LL;

int n, m, pos;
char str[maxn];
list<char> ml;
stack<char> ms;

void init() {
ml.clear();
}

int main() {
while (scanf("%d%d%d", &n, &m, &pos) == 3 && n) {
pos--;
init();
scanf("%s", str);
for (int i = 0; i < n; i++) {
ml.push_back(str[i]);
}
scanf("%s", str);
list<char>::iterator it = ml.begin();
while (pos--) ++it;
for (int i = 0; i < m; i++) {
if (str[i] == 'L') it--;
else if (str[i] == 'R') it++;
else {
ms.push(*it);
list<char>::iterator p = it;
if (*p == '(') {
while (!ms.empty()) {
++p;
if (ms.top() == '('&& *p == ')') ms.pop();
else ms.push(*p);
}
++p;
it = ml.erase(it, p);
}
else {
while (!ms.empty()) {
--p;
if (*p == '('&&ms.top() == ')') ms.pop();
else ms.push(*p);
}
++it;
it = ml.erase(p, it);
}
if (it == ml.end()) --it;
}
}
for (it = ml.begin(); it != ml.end(); it++) printf("%c", *it);
printf("\n");
}
return 0;
}


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: