题目大意:
http://codevs.cn/problem/1009/
代码:
#include <iostream> using namespace std; string n; int k,a,b; int arr[10][10] = {0}; int main() { cin >> n >> k; for(int i = 1; i <= k; i++) { cin >> a >> b; arr[a][b] = 1; } /* for(int i = 0; i < 10; i++) for(int j = 0; j < 10; j++) for(int m = 0; m < 10; m++) if(i != j && j != m && m != i && arr[i][j] == 1 && arr[j][m] == 1) arr[i][m] = 1 ;*/ for(int i = 0; i < 10; i++) { for(int j = 0; j < 10; j++) { if(arr[j][i] == 1) { for(int m = 0; m < 10; m++) { if(arr[i][m] == 1) arr[j][m] = 1; } } } } long long res = 1; for(int i = 0; i < n.length();i++) { int tmp = n[i]-'0'; int count = 1; for(int j = 0; j < 10; j++) { if(arr[tmp][j] == 1 && tmp != j) count++; } res *= count; } cout << res << endl; return 0; }