时间限制 1000 ms 内存限制 65536 KB

题目描述

Saber is fond of number games of various kinds, she particularly likes a kind of number-guessing game. The game is played in pairs, one player decides an integer X without leading zeros, and writes it down on the paper twice with part of its numbers coded by upper case letters. The rule goes that the same letter can only represent same digit. For example, the number 1010 can be written in the form of 1XYX and 1A10, or AB10 and C0AB. The other player has to guess the original number. In both cases, 1010 is the only answer.

Unfortunately, in most cases, there are more than one answer to the question, sometimes due to miscoding there is just no solution at all. Saber wants to know the total number of integers that can be the correct answer to the codes written on the paper, so that she can figure out how many times at most she would guess.

输入格式

The input begins with a line containing a single integer T(1≤T≤200), indicating the number of test cases. For each test case, the first line contains one integers N, indicating the number of digits of the integer to be guessed. The next two lines each contains a string containing N(2≤N≤18) characters, describing the codes written on the paper. It is guaranteed that the strings would only contain digits and upper case letters.

输出格式

For each test case, print a single line with one integer, indicating the number of integers that can be the correct answer. If there are no solutions, please output '0' without quotes!

输入样例

3
4
1XYX
1A10
3
XXX
YYY
8
TOOYOUNG
TOONAIVE

输出样例

1
9
90000

题意是给两个字符串,上下对应位置的字母和数字之间相互等同,没有数字对应的字母可以等价于任意数字,但字符串整体来看不能有前导0,求所有的字符串可能代表的数字的数量,例如

1XYX

1A10

X等价于A,Y等于1,X等于0,所有A等于0,所以答案是1,因为只有一种可能性。

XXX

YYY

X等价于Y,然而他们可以等价于任意数字,但是不能等于0,不然就变成了000,不符合要求,所以答案是9

TOOYOUNG

TOONA I VE

Y等于N,O等于A,U等于I,N等于V,所以Y等于N等于V,G等于E,首字母T只能等于1-9,O和A任意0-10,Y、N、V任意0-10,U和I任意0-10,G和E任意0-10,这样9*10*10*10*10=90000

最先想到的是并查集,或者直接bfs搜索也行。

初始化parent[i]=i

从字符串位置0开始,同时检查两个字符串相同位置,如果是数字而且不同,那么直接answer=0;如果是数字和字母,那么让字母的parent等于数字;如果都是字母,跑并查集,检查双方的parent,如果都已经有数字,但是不同,那么直接answer=0,如果不是,将靠后字母的parent设为靠前字母的parent。当位置0的时候,给字符打一个起始位置的标记sig=1,以便后来检查前导0。

最后,设answer=1,再从0开始,检查当前位的字母的parent,如果是数字,那就*1;如果是数字0,那就answer=0;如果是字母而且是自己本身而且没有使用过,那就*10(比如A=B,字符串为AB,那么在A的位置上*10,在B的位置上就不再处理了,因为A=B);但如果是开头第一位,那就*9。

#include<cstdio>
#include<cmath>
#include<cstdlib>
#include<cstring>
#include<string>
#include<algorithm>
#define clr0(a) memset(a,0,sizeof a)
#define clr1(a) memset(a,1,sizeof a)
#define ini(a) memset(a,-1,sizeof a)
#define N 150using namespace std;char a[50],b[50];
int n,pa[N],vis[N],sig[N],rec[N];int f(int x){return pa[x]==x?x:pa[x]=f(pa[x]);}
int main(){int i,t,j,n;for(scanf("%d",&t);t--;){clr0(vis);clr0(sig);ini(rec);scanf("%d",&n);scanf("%s%s",a,b);for(i=0;i<150;i++){pa[i]=i;}for(i=48;i<58;i++){rec[i]=i-48;}long long res=1;for(i=0;i<n;i++){int px,py;if(a[i]<='9'&&a[i]>='0'&&b[i]<='9'&&b[i]>='0'&&a[i]!=b[i]){res=0;goto out;}else{px=f(a[i]);py=f(b[i]);if(px!=py){if(rec[px]!=rec[py]&&rec[px]!=-1&&rec[py]!=-1){res=0;goto out;}else{pa[py]=px;rec[px]=rec[px]>rec[py]?rec[px]:rec[py];if(sig[py]==1)sig[px]=1;}}}if(i==0){sig[px]=1;}}if(rec[f(a[0])]==0){res=0;goto out;}for(i=0;i<n;i++){if(f(a[i])==a[i]){if(vis[a[i]]==0){if(rec[a[i]]==-1){if(sig[a[i]]==1){res=res*9;}else{res=res*10;}}vis[a[i]]=1;}}}out:printf("%lld\n",res);}return 0;
}

北邮OJ 981. 16校赛-Saber's Number Game相关推荐

  1. 北邮OJ 1022. 16校赛-Saber's Board

    时间限制 5000 ms 内存限制 65536 KB 题目描述 In a parallel universe, Saber has won the champion of every kind of ...

  2. 北邮OJ 1027. 16校赛-Archer in Archery

    时间限制 1000 ms 内存限制 65536 KB 题目描述 Archer(Emiya), also known as the red A, is famous for his talented s ...

  3. 北邮OJ 1021. 16校赛-Stone Game

    时间限制 4000 ms 内存限制 65536 KB 题目描述 Alice and Bob are old friends in game theory. This afternoon they me ...

  4. 北邮OJ 1010. 16校赛-Binary Strings

    时间限制 5000 ms 内存限制 65536 KB 题目描述 One day, the teacher asked Weishen to judge whether a binary string ...

  5. 北邮OJ 1005. 16校赛-Hawei Learning C

    时间限制 1000 ms 内存限制 65536 KB 题目描述 Hawei is learning C programming language recently, but he is so naiv ...

  6. 北邮OJ 980. 16校赛-R_clover's Challenge

    时间限制 2000 ms 内存限制 65536 KB 题目描述 R_clover wants to challenge Mengmengda_wsw's math,so he give her a f ...

  7. 北邮OJ 884. 16校赛-Average Modulo

    时间限制 5000 ms 内存限制 65536 KB 题目描述 We define function g on an array as: g([a0,a1,⋯,an−1])=(Σn−1l=0al) m ...

  8. 北邮oj题库刷题计划(更新ing)

    北邮oj题库刷题计划(更新ing) 83. A + B Problem 84 Single Number 85. Three Points On A Line 120 日期 121 最值问题 122 ...

  9. 北邮OJ 141 虚数

    北邮OJ 虚数 #include <bits/stdc++.h> using namespace std; typedef struct fushu{int x; //实部 int y; ...

最新文章

  1. 英语发音规则---N字母
  2. 怎样安全的使用可变参数宏__VA_ARGS__
  3. 优雅还不够,简洁才高效!——用NValidator一句话搞定客户端检测
  4. 推荐15个让新手爱不释手的Python高级库
  5. onDraw什么时候被调用?
  6. CSS中实现水平/垂直居中
  7. rocketmq批量消费
  8. spring.shardingsphere.rules.sharding.sharding-algorithms.database_inline.props‘ is not valid
  9. [资料]PHP中的__set __get使用
  10. larveral 直接拷贝安装_做一个能引导所有系统的安装盘
  11. 语法制导定义 SDD
  12. 卸载ps显示无法连接adobe服务器,PS还在无法安装?无法卸载?通通搞定!
  13. 读书感受 之 《穷查理宝典》
  14. 用x360ce,北通蓝牙手柄成功玩双人成行
  15. java 银行卡归属地查询_银行卡归属地查询示例代码
  16. 医院管理系统软件的全球与中国市场2022-2028年:技术、参与者、趋势、市场规模及占有率研究报告
  17. Excel中取值函数三剑客:LEFT、RIGHT、MID
  18. 火遍全网的chatGPT(文末有彩蛋)
  19. 计网——17差错检测和纠正技术
  20. SpringBoot集成RedisTemplate

热门文章

  1. Python+人工智能的超强组合,再不学就跟不上时代啦!
  2. centos 6.5安装mysql5.7,centos6.5安装mysql5.7
  3. poi向word插入图片_如何使用word裁剪图片图形?如何使用word修整图片?
  4. 【虚拟化】docker构建私有仓库,上传镜像至私有仓库
  5. 带你深入理解值传递(点进来才知道它是一篇使你收益的文章)
  6. SpringMVC-快速入门
  7. Elasticsearch系列「」学习路线
  8. Transformation
  9. matlab canny边缘,matlab – 定向Canny边缘检测
  10. 大数据 智能交通调度_大数据技术在智能交通中的应用