题目

字符串模拟+并查集

建立两个并查集分别存放每个变量的每一位数的祖先,一个是1一个是2

考虑每个字母的每一位的数都是唯一的,先模拟,记录每一个变量的每一位。

一一映射到方程中去,最后将两个方程进行一一比较,然后合并并查集。中间判断是否出现一位既是1又是2的情况

最后统计自由元的个数cnt,高精求解2^cnt

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#define int long long
#define N 100100
using namespace std;
int k, tot, len, len1, len2, tot1, tot2;
int x[N], y[N], sum[N], fa[N], jin[N];      //sum[i]表示i所代表的字母的开始位置
string s1, s2;
int find(int a)
{
    if (a == fa[a]) return a;
    return fa[a] = find(fa[a]);
}
signed main()
{
    scanf("%lld", &k);
    sum[1] = len = 2;
    for (int i = 2; i <= k + 1; i++)
        scanf("%lld", &len), sum[i] = sum[i - 1] + len, tot += len;
    cin >> s1 >> s2;
    int len1 = s1.size(), len2 = s2.size();
    for (int i = 0; i < len1; i++)
    {
        if (s1[i] >= 'a' && s1[i] <= 'z')
        {
            int now = s1[i] - 'a' + 1;
            for (int j = sum[now]; j < sum[now + 1]; j++)
                x[++tot1] = j;
        }
        else x[++tot1] = s1[i] - '0';
    }
    for (int i = 0; i < len2; i++)
    {
        if (s2[i] >= 'a' && s2[i] <= 'z')
        {
            int now = s2[i] - 'a' + 1;
            for (int j = sum[now]; j < sum[now + 1]; j++)
                y[++tot2] = j;
        }
        else y[++tot2] = s2[i] - '0';
    }
    if (tot1 != tot2)
        printf("0"), exit(0);
    for (int i = 1; i <= tot1 * 4; i++)
        fa[i] = i;
    for (int i = 1; i <= tot1; i++)
    {
        int a = x[i], b = y[i];
        if (a + b == 1)         //如果a,b不相等,就直接输出不行
            printf("%lld\n", 0), exit(0);
        int da = find(a), db = find(b);
        if (da + db == 1)
            printf("0"), exit(0);
        if (da != db)           //已经排除了a,b等于1或0的情况
            fa[da] = db, tot--;
}
    int big[100010]= {1}, top=1;
    for(int i=tot; i>=1; i--)
    {
        for(int i=0; i<top; ++i)big[i]<<=1;
        for(int i=0; i<top; ++i)if(big[i]>=10)
            {
                big[i+1]+=big[i]/10,big[i]%=10;
            }
        for(; big[top]; ++top)
        {
            big[top+1]+=big[top]/10,big[top]%=10;
        }
    }
    for(int i=top-1; i>=0; --i) printf("%lld", big[i]);
    return 0;
}
/*
5
4 2 4 4 2
1bad1
acbe

*/
02-10 10:34