日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

北邮OJ 981. 16校赛-Saber's Number Game

發(fā)布時間:2024/9/30 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 北邮OJ 981. 16校赛-Saber's Number Game 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

時間限制?1000 ms?內(nèi)存限制?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(1T200), 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(2N18)?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 題意是給兩個字符串,上下對應(yīng)位置的字母和數(shù)字之間相互等同,沒有數(shù)字對應(yīng)的字母可以等價于任意數(shù)字,但字符串整體來看不能有前導(dǎo)0,求所有的字符串可能代表的數(shù)字的數(shù)量,例如

1XYX

1A10

X等價于A,Y等于1,X等于0,所有A等于0,所以答案是1,因為只有一種可能性。

XXX

YYY

X等價于Y,然而他們可以等價于任意數(shù)字,但是不能等于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開始,同時檢查兩個字符串相同位置,如果是數(shù)字而且不同,那么直接answer=0;如果是數(shù)字和字母,那么讓字母的parent等于數(shù)字;如果都是字母,跑并查集,檢查雙方的parent,如果都已經(jīng)有數(shù)字,但是不同,那么直接answer=0,如果不是,將靠后字母的parent設(shè)為靠前字母的parent。當(dāng)位置0的時候,給字符打一個起始位置的標(biāo)記sig=1,以便后來檢查前導(dǎo)0。


最后,設(shè)answer=1,再從0開始,檢查當(dāng)前位的字母的parent,如果是數(shù)字,那就*1;如果是數(shù)字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; }

總結(jié)

以上是生活随笔為你收集整理的北邮OJ 981. 16校赛-Saber's Number Game的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。