Codeforces Round 913 (Div. 3)B(预处理)

发布时间 2023-12-06 01:46:26作者: 拍手称快

[Codeforces Round 913 (Div. 3)B]{https://codeforces.com/contest/1907}

预处理:

如果说一个数据是静态的,那可用预处理来减少运行时间。
在这里,我们对字符串的大小写字母做一个预处理,提前用数组记录下他们的位置,然后再根据题目要求对对应位置做标记删除就好,这种方法可以将复杂度从o^2降低至o;

const:

用const 将一个变量量转化成具体数据,方便修改

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int N = 1e6 + 10;

int a[N];
int b[N];
int c[N];

int main() {
	int t;
	cin >> t;
	for (int j = 1; j <= t; j++) {
		string n;
		cin >> n;
		int b1 = 0;
		int c1 = 0;
		memset(a, 0, sizeof(a));
		for (int i = 0; i < n.length(); i++) {

			if (n[i] == 'B') {
				a[i] = -1;
				if (c1 >= 1) {
					a[c[c1--]] = -1;
				}

			} else if (n[i] == 'b') {
				a[i] = -1;
				if (b1 >= 1) {
					a[b[b1--]] = -1;
				}
			} else if (n[i] >= 'a' && n[i] <= 'z' ) {
				b[++b1] = i;
			} else  {
				c[++c1] = i;
			}
		}


		for (int i = 0; i < n.length(); i++) {
			if (a[i] != -1) {
				cout << n[i];
			}
		}
		cout << endl;
	}

	return 0;
}