首页 > 代码库 > codevs1009 产生数

codevs1009 产生数

题目描述 Description

  给出一个整数 n(n<10^30) 和 k 个变换规则(k<=15)。
  规则:
   一位数可变换成另一个一位数:
   规则的右部不能为零。
  例如:n=234。有规则(k=2):
    2-> 5
    3-> 6
  上面的整数 234 经过变换后可能产生出的整数为(包括原数):
   234
   534
   264
   564
  共 4 种不同的产生数
问题:
  给出一个整数 n 和 k 个规则。
求出:
  经过任意次的变换(0次或多次),能产生出多少个不同整数。
  仅要求输出个数。

输入描述 Input Description

键盘输人,格式为:
   n k
   x1 y1
   x2 y2
   ... ...
   xn yn

输出描述 Output Description

 屏幕输出,格式为:
  一个整数(满足条件的个数)

样例输入 Sample Input


   234 2
   2 5
   3 6

样例输出 Sample Output

4

思路:
floyd预处理一个数能变换成多少数
代码:
#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<cstdlib>#include<algorithm>using namespace std;const int maxn = 1000;char temp[maxn];int edge[maxn][maxn],cou[maxn],n,k,big[maxn];void init(){    cin>>temp>>k;    n = strlen(temp);    for(int i = 0;i <= 9;i++){        for(int j = 0;j <= 9;j++){            if(i == j) edge[i][j] = 1;            else edge[i][j] = 0;        }    }    for(int i = 0;i < k;i++){        int u,v;        cin>>u>>v;        edge[u][v] = 1;    }    big[0] = 1;    }void fshort(){    for(int k = 0;k <= 9;k++){        for(int i = 0;i <= 9;i++){            if(i != k){                for(int j = 0;j <=9;j++)                    if(j != i && j != k &&(edge[i][k] && edge[k][j])) edge[i][j] = 1;                            }        }    }    for(int i = 0;i <= 9;i++)        for(int j = 0;j <= 9;j++)           cou[i] += edge[i][j];        }void mplus(){    int ans = 1,a,b = 0;    for(int i = 0;i < n;i++){        if(cou[temp[i] - 48]){            a = cou[temp[i] - 48];            for(int j = b;j >= 0;j--){                big[j] *= a;                if(big[j] > 9){                    big[j + 1] += big[j] / 10;                    big[j] = big[j] % 10;                    if(j == b) b++;                }            }        }                }    for(int j = 0;j <= b;j++){                if(big[j] > 9){                    big[j + 1] += big[j] / 10;                    big[j] = big[j] % 10;                    if(j == b) b++;                }    }    for(int r = b;r >= 0;r--)cout<<big[r];}int main(){    init();    fshort();    mplus();    return 0;}

 

codevs1009 产生数