萌新求调代码
  • 板块CF1671E Preorder
  • 楼主Kobe303
  • 当前回复7
  • 已保存回复7
  • 发布时间2022/5/19 20:47
  • 上次更新2023/10/28 01:06:25
查看原帖
萌新求调代码
292300
Kobe303楼主2022/5/19 20:47

做法是 Hash 判子树是否同构。

一直 WA on #28。

求大佬帮我看看是 Hash 被卡了还是其他地方不对

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N = 262150, mod = 998244353;
ll P = 132423247;
int n, m, ans;
char s[N];
ll f[N], p[N];

int qpow(int x, int y) {
	int res = 1;
	while (y) {
		if (y & 1) res = 1ll * res * x % mod;
		x = 1ll * x * x % mod;
		y >>= 1;
	}
	return res;
}

ll calc(int l, int r) {
	return (((f[r] - f[l - 1] * p[r - l + 1] % P) % P) + P) % P;
}

int main() {
	scanf("%d%s", &m, s + 1);
	n = (1 << m) - 1;
	p[0] = 1;
	for (int i = 1; i <= n; ++i) {
		f[i] = (f[i - 1] * 131 + (s[i] - 'A' + 1)) % P;
		p[i] = p[i - 1] * 131 % P;
	}
	for (int i = 1; i <= n / 2; ++i) {
		//枚举非叶子节点
		int ls = i << 1, rs = i << 1 | 1;
		int len = 1;
		while (1) {
			if (ls > n) break;
			if (calc(ls, ls + len - 1) != calc(rs, rs + len - 1)) {
				++ans;
//				cout << ls << " " << rs << " " << len << endl;
				break;
			}
			ls <<= 1, rs <<= 1, len <<= 1;
		}
	}
	printf("%d", qpow(2, ans));
	return 0;
}
2022/5/19 20:47
加载中...