比赛 4043级NOIP2022欢乐赛4th 评测结果 AAAAAWA
题目名称 破译密文 最终得分 85
用户昵称 yrtiop 运行时间 0.000 s
代码语言 C++ 内存使用 0.00 MiB
提交时间 2022-11-07 20:18:22
显示代码纯文本
#include <bits/stdc++.h>
#define pb emplace_back
using i64 = long long;

const int maxn = 1e6 + 5;
std::vector<int> G[2];
char t[200],s[2][maxn];
bool vis[200];
int num,pre[maxn];

int get(int x,int y) {
	if(x <= 1)return x;
	else return (x - 'a' + 1) * 100 + y;
}

int find(int x) {
	return x == pre[x] ? x : pre[x] = find(pre[x]);
}

i64 power(i64 x,i64 y) {
	i64 ans = 1;
	for(;y;y >>= 1) {
		if(y & 1)(ans *= x);
		(x *= x);
	}
	return ans;
}

int main() {
	freopen("encrypt.in","r",stdin);
	freopen("encrypt.out","w",stdout);
	scanf("%s %s",s[0] + 1,s[1] + 1);
	int Q;
	scanf("%d",&Q);
	int cnt = 0;
	while(Q --) {
		char c;
		int x;
		scanf(" %c %d",&c,&x);
		t[c] = x;
	}
	num = 2;
	for(int j = 0;j < 2;++ j) {
		int n = strlen(s[j] + 1);
		for(int i = 1;i <= n;++ i) {
			if(s[j][i] <= '1')G[j].pb(get(s[j][i] ^ '0' , 0));
			else {
				if(!vis[s[j][i]]) {
					vis[s[j][i]] = true;
					cnt += t[s[j][i]];
				}
				for(int k = 1;k <= t[s[j][i]];++ k) {
					G[j].pb(get(s[j][i] , k));
					num = std::max(num , get(s[j][i] , k));
				}
			}
		}
	}
	if(G[0].size() != G[1].size()) {
		puts("0");
		return 0;
	}
	for(int i = 0;i <= num;++ i)
		pre[i] = i;
	int n = G[0].size();
	for(int i = 0;i < n;++ i) {
		int u = find(G[0][i]),v = find(G[1][i]);
		if(u == v)continue ;
		if(u <= 1&&v <= 1) {
			puts("0");
		}
		if(u <= 1) {
			pre[v] = u;
			-- cnt;
			continue ;
		}
		if(v <= 1) {
			pre[u] = v;
			-- cnt;
			continue ;
		}
		pre[v] = u;
		-- cnt;
	}
	printf("%lld\n",power(2 , cnt));
	return 0;
}