描述
给出一个整数 n(n<10^30) 和 k 个变换规则(k<=15)。
规则:
一位数可变换成另一个一位数:
规则的右部不能为零。
例如:n=234。有规则(k=2):
2-> 5
3-> 6
上面的整数 234 经过变换后可能产生出的整数为(包括原数):
234
534
264
564
共 4 种不同的产生数
问题:
给出一个整数 n 和 k 个规则。
求出:
经过任意次的变换(0次或多次),能产生出多少个不同整数。
仅要求输出个数。
输入
n k
x1 y1
x2 y2
… …
xn yn
输出
一个整数(满足条件的个数)。
输入样例 1
234 2
2 5
3 6
输出样例 1
4
有几点要注意:
- 每个位置上的数可以一直转换(条件允许的话);
- 可以变换的得到的个数就是每个位置上可以变换的数量相乘
- 得到的答案long long不够用,需要手写高精度乘法
#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<queue>
#include<vector>
#include<map>
using namespace std;
const int N=15;
const int inf=0x3f3f3f3f;
char s[100];
int v[N],tot,f,a[N],b[N];
int ans[100]={1},top=0;
void dfs(int x){
v[x]=1;
tot++;
for(int i=0;i<f;i++){
if(a[i]==x&&v[b[i]]==0){
dfs(b[i]);
}
}
}
void mult(int x){
int n=0;
for(int i=0;i<=top;i++){
ans[i]=ans[i]*x+n;
n=ans[i]/10;
ans[i]=ans[i]%10;
}
while(n){
ans[++top]=n%10;
n/=10;
}
}
int main()
{
int i,j,k,x,y;
scanf("%s %d",&s,&f);
for(i=0;i<f;i++){
scanf("%d %d",&a[i],&b[i]);
}
for(i=0;s[i];i++){
memset(v,0,sizeof(v));
tot=0;
dfs(s[i]-'0');//得出每个位置上可以变换得到几个数
mult(tot);
}
for(i=top;i>=0;i--){
printf("%d",ans[i]);
}
return 0;
}