Confidential Intelligence P2106
Problem Description
HY is very fond of chatting with GJQ, while others are still struggling in the world of OI. To avoid disturbing students, they exchange encrypted messages. The plaintext used for communication is a non-empty sequence made up of 0s and 1s. Each cipher letter represents a unique binary string.
For example, the ciphertext "011a0bf00a01" contains several password letters that each correspond to a different binary string. The key to deciphering these messages lies in determining what each cipher letter stands for.
After long-term statistical analysis, it is now known that each password has a fixed length. Students who have experienced the pain of decryption have intercepted two ciphertexts, S1 and S2, and know that S1 equals S2, meaning they represent the same plaintext. Your task is to determine how many possible plaintexts could exist based on the given ciphertexts.
Input Format

Output Format
M (indicating the number of possible plaintexts)
Sample Input
100ad1
Cc1
4
a 2
d 3
c 4
b 50
Sample Output
2
Hint
The length of the plaintext is ≤ 10000, ensuring no high precision is required.
This question is actually quite straightforward. The approach involves identifying and merging positions with the same value, grouping positions with value 0 into one set, and those with value 1 into another. Positions that are not in either group are considered undetermined. The total number of such undetermined positions determines the number of possible solutions, which is calculated as 2^n. However, if there is an inconsistency where a position is both 0 and 1, then there is no solution.
Code:
#include "stdio.h"
#include
#include "algorithm"
#include
#include "vector"
#define N 55555
#define ll unsigned long long
using namespace std;
const ll T = 40000;
string s1, s2;
vector P[233];
char A[233];
ll n, L[233], sum1[N], sum2[N], F[N];
bool mark[N];
ll GF(ll x) {
if (F[x] != x) F[x] = GF(F[x]);
return F[x];
}
void Merge(ll x, ll y) {
ll fx = GF(x), fy = GF(y);
if (fx != fy) F[fx] = fy;
}
ll QM(ll a, ll b) {
ll ans = 1;
while (b) {
if (b & 1) ans = ans * a;
b >>= 1; a = a * a;
}
return ans;
}
int main() {
ll i, j, k, ans; ans = 0;
cin >> s1 >> s2;
s1 = " " + s1; s2 = " " + s2;
scanf("%lld", &n);
for (i = 1; i <= n; i++) scanf("%c %lld", &A[i], &k), L[A[i]] = k;
for (i = 1; i <= T; i++) F[i] = i;
for (i = 1; i < s1.length(); i++) {
if (s1[i] == '0') sum1[i] = sum1[i - 1] + 1, Merge(sum1[i], T);
else if (s1[i] == '1') sum1[i] = sum1[i - 1] + 1, Merge(sum1[i], T + 1);
else sum1[i] = sum1[i - 1] + L[s1[i]], P[s1[i]].push_back(sum1[i - 1]);
}
for (i = 1; i < s2.length(); i++) {
if (s2[i] == '0') sum2[i] = sum2[i - 1] + 1, Merge(sum2[i], T);
else if (s2[i] == '1') sum2[i] = sum2[i - 1] + 1, Merge(sum2[i], T + 1);
else sum2[i] = sum2[i - 1] + L[s2[i]], P[s2[i]].push_back(sum2[i - 1]);
}
for (i = 1; i <= n; i++)
for (j = 1; j <= L[A[i]]; j++)
for (k = 1; k <= P[A[i]].size(); k++)
Merge(P[A[i]][k - 1] + j, P[A[i]][k] + j);
for (i = 1; i <= sum1[s1.length() - 1]; i++)
if (GF(i) != GF(T) && GF(i) != GF(T + 1) && (!mark[GF(i)])) ans++, mark[GF(i)] = 1;
if (GF(T) != GF(T + 1)) printf("%lld", QM(2, ans));
else printf("0");
}Nano Oval Core
Anyang Kayo Amorphous Technology Co.,Ltd. , https://www.kayoamotech.com