比赛 |
4043级NOIP2022欢乐赛4th |
评测结果 |
AAAAAAA |
题目名称 |
破译密文 |
最终得分 |
100 |
用户昵称 |
该账号已注销 |
运行时间 |
0.000 s |
代码语言 |
C++ |
内存使用 |
0.00 MiB |
提交时间 |
2022-11-07 21:14:53 |
显示代码纯文本
#include <bits/stdc++.h>
using namespace std;
string s1, s2;
int ss1[20010], ss2[20010], f[20010];
int le[27], js[27][110];
int n;
int len1 = 0, len2 = 0;
int p = 0;
bool ky[20010];
unsigned long long ans = 1;
int fd(int x) {
if (f[x] == x)
return x;
return f[x] = fd(f[x]);
}
unsigned long long power(int x, int y) {
if (y == 0)
return 1;
if (y == 1)
return x;
if (y % 2 == 0) {
unsigned long long q = power(x, y / 2);
return q * q;
}
return power(x, y - 1) * x;
}
int main() {
freopen("encrypt.in", "r", stdin);
freopen("encrypt.out", "w", stdout);
cin >> s1 >> s2;
cin >> n;
int k = 1;
for (int i = 1; i <= n; i++) {
char x;
cin >> x;
cin >> le[x - 'a' + 1];
for (int j = 1; j <= le[x - 'a' + 1]; j++)
js[x - 'a' + 1][j] = ++k;
}
for (int i = 0; i <= k; i++)
f[i] = i;
for (int i = 0; i < s1.length(); i++) {
if (s1[i] == '1') {
ss1[++len1] = 1;
} else if (s1[i] == '0')
ss1[++len1] = 0;
else {
int cnt = le[s1[i] - 'a' + 1];
int m = 0;
while (cnt > 0) {
ss1[++len1] = js[s1[i] - 'a' + 1][++m];
ky[ss1[len1]] = 1;
cnt--;
}
}
}
for (int i = 0; i < s2.length(); i++) {
if (s2[i] == '1') {
ss2[++len2] = 1;
} else if (s2[i] == '0')
ss2[++len2] = 0;
else {
int cnt = le[s2[i] - 'a' + 1];
int m = 0;
while (cnt > 0) {
ss2[++len2] = js[s2[i] - 'a' + 1][++m];
ky[ss2[len2]] = 1;
cnt--;
}
}
}
if (len1 != len2) {
cout << 0 << endl;
return 0;
}
for (int i = 1; i <= len1; i++) {
int x, y, fx, fy;
x = ss1[i], y = ss2[i];
fx = fd(x);
fy = fd(y);
if (fx == 0 && fy == 1 || fx == 1 && fy == 0) {
cout << 0 << endl;
return 0;
}
if (fx == 1 || fx == 0)
f[fy] = fx;
else
f[fx] = fy;
}
for (int i = 2; i <= k; i++) {
if (f[i] == i && ky[i] == 1) {
p++;
}
}
cout << power(2, p) << endl;
return 0;
}