社区讨论

如何优化哈希?

P9753[CSP-S 2023] 消消乐参与者 2已保存回复 1

讨论操作

快速查看讨论及其快照的属性,并进行相关操作。

当前回复
1 条
当前快照
1 份
快照标识符
@mhj3f9ih
此快照首次捕获于
2025/11/03 20:05
4 个月前
此快照最后确认于
2025/11/03 20:05
4 个月前
查看原帖
爆在性质A,即随机
CPP
#include <iostream>
#include <cstdio>
#include <string> 
#include <unordered_map>

namespace noip {
typedef long long Int;

constexpr Int MAX_N = 2000000;
Int n, ans;
char a[MAX_N+2];
std::string p;
std::unordered_map<std::string, Int> m;

void main() {
	//freopen("input.in", "r", stdin);
	//freopen("output.out", "w", stdout);
	std::cin >> n >> (a+1);
	
	// 开一个栈 模拟进出 
	// 如果某一时刻的栈完全相等 则说明中间的一段被消掉了
	// 令某个栈形的数量为 i 
	// 最终值为 SUM(i(i-1)/2)
	
	p = ""; m[""] = 1;
	for (Int i = 1; i <= n; i++) {
		if (p == "") p += a[i];
		else if (a[i] == p[p.size()-1]) p.erase(p.end()-1);
		else p += a[i];
		
		if (m.find(p) == m.end()) m[p] = 1;
		else m[p]++;
	}
	
	ans = 0;
	for (auto& r : m) 
		ans += (r.second-1)*r.second/2;
	
	std::cout << ans;
}
} int main() {
	noip::main();
	return 0;
}

回复

1 条回复,欢迎继续交流。

正在加载回复...