求分析时间复杂度
  • 板块学术版
  • 楼主wwqwq
  • 当前回复6
  • 已保存回复6
  • 发布时间2023/10/22 21:07
  • 上次更新2023/11/2 11:47:51
查看原帖
求分析时间复杂度
669919
wwqwq楼主2023/10/22 21:07

rt,CSP-S T2

#include <bits/stdc++.h>

using namespace std;

char a[2000005];
char b[2000005][2];
int top, top2;
int n, ans = 0;
bool h = 1;

int sc() {
	int res = 0;
	for (int i = 1; i <= top; i++) {
		if (b[i][!h] == b[i + 1][!h] && i != top) {
			i++;
			res += 2;
		}
		else b[++top2][h] = b[i][!h];
	}
	top = top2;
	top2 = 0;
	h = !h;
	return res;
}

int main() {
	freopen("game.in", "r", stdin);
	freopen("game.out", "w", stdout);
	cin >> n;
	for (int i = 1; i <= n; i++) cin >> a[i];
	for (int i = 1; i <= n; i++) {
		for (int j = i + 1; j <= n; j++) {
			if ((j - i + 1) % 2 != 0) continue;
			top = 0;
			top2 = 0;
			h = 1;
			int zs = j - i + 1;
			for (int k = i; k <= j; k++) {
				b[++top][0] = a[k];
			}
			while (1) {
				int fh = sc();
				zs -= fh;
//				cout << fh << endl;
				if (zs == 0) {
					ans++;
//					cout << i << ' ' << j << endl;
					break;
				}
				if (fh == 0) break;
			}
		}
	}
	cout << ans << endl;
	return 0;
}
2023/10/22 21:07
加载中...