题解:
x->yyy
其实就是把x代替成yyy这个值。
如果不好理解的话, 可以试想一下, 刚开始的话 0->0, 1->1, 2->2,...,9->9.
现在有一条指令 1->23
那么就是就是0->0, 1->23, 2->2,...,9->9.
现在又有一条指令2->45
那么就相当于0->0, 1->453, 2->45,...,9->9.
就相当于我们求出0~9每个数字分别代表的是什么东西。
然后我们倒着DP给定的指令。
代码:
#include<bits/stdc++.h> using namespace std; #define Fopen freopen("_in.txt","r",stdin); freopen("_out.txt","w",stdout); #define LL long long #define ULL unsigned LL #define fi first #define se second #define pb push_back #define lson l,m,rt<<1 #define rson m+1,r,rt<<1|1 #define lch(x) tr[x].son[0] #define rch(x) tr[x].son[1] #define max3(a,b,c) max(a,max(b,c)) #define min3(a,b,c) min(a,min(b,c)) typedef pair<int,int> pll; const int inf = 0x3f3f3f3f; const int _inf = 0xc0c0c0c0; const LL INF = 0x3f3f3f3f3f3f3f3f; const LL _INF = 0xc0c0c0c0c0c0c0c0; const LL mod = (int)1e9+7; const int N = 1e5 + 100; char s[N]; string str[N]; LL dp[15], base[15]; void solve(string & sstr){ int m = sstr[0] - '0'; int len = sstr.size(); LL tmp_base = 1; LL tmp_yu = 0; for(int i = 3; i < len; ++i){ int id = sstr[i] - '0'; tmp_base = (tmp_base * base[id]) % mod; tmp_yu = (tmp_yu * base[id] + dp[id]) % mod; } dp[m] = tmp_yu, base[m] = tmp_base; // cout << m << ' ' << dp[m] << ' ' << base[m] << endl; } int main(){ scanf("%s", s+1); int n; scanf("%d", &n); for(int i = 0; i < 10; ++i) dp[i] = i, base[i] = 10; for(int i = 1; i <= n; ++i){ cin >> str[i]; } for(int i = n; i >= 1; --i){ solve(str[i]); } int m = strlen(s+1); LL ans = 0; for(int i = 1; i <= m; ++i){ int id = s[i] - '0'; ans = (ans * base[id] + dp[id]) % mod; } cout << ans << endl; return 0; }