codevs 1009 产生数

1009 产生数

 

2002年NOIP全国联赛普及组

 时间限制: 1 s
 空间限制: 128000 KB
 题目等级 : 黄金 Gold
 
 
 
题目描述 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

数据范围及提示 Data Size & Hint
 

思路:flyed+组合数学

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
using namespace std;
string s;
int k,a,b;
bool dis[11][11];
long long sum=1;
int main(){
    cin>>s>>k;
    for(int i=1;i<=k;i++){
        cin>>a>>b;
        dis[a][b]=1;
    }
    for(int k=0;k<=9;k++)
        for(int i=0;i<=9;i++)
            for(int j=0;j<=9;j++)
                if(i!=j&&j!=k&&k!=i&&dis[i][k]==1&&dis[k][j]==1)
                    dis[i][j]=1;
    for(int i=0;i<s.length();i++) {
        int temp=s[i]-'0',num=1;
        for(int j=0;j<=9;j++)
            if(dis[temp][j]==1&&temp!=j)    num++;
        sum*=num;
    }
    cout<<sum;
}
细雨斜风作晓寒。淡烟疏柳媚晴滩。入淮清洛渐漫漫。 雪沫乳花浮午盏,蓼茸蒿笋试春盘。人间有味是清欢。
原文地址:https://www.cnblogs.com/cangT-Tlan/p/7709990.html