日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

Codeforces Round #265 (Div. 1) C. Substitutes in Number dp

發(fā)布時(shí)間:2025/5/22 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Codeforces Round #265 (Div. 1) C. Substitutes in Number dp 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

題目鏈接:

http://codeforces.com/contest/464/problem/C

J. Substitutes in Number


time limit per test 1 secondmemory limit per test 256 megabytes

問題描述

Andrew and Eugene are playing a game. Initially, Andrew has string s, consisting of digits. Eugene sends Andrew multiple queries of type "di?→?ti", that means "replace all digits di in string s with substrings equal to ti". For example, if s?=?123123, then query "2?→?00" transforms s to 10031003, and query "3?→?" ("replace 3 by an empty string") transforms it to s?=?1212. After all the queries Eugene asks Andrew to find the remainder after division of number with decimal representation equal to s by 1000000007 (109?+?7). When you represent s as a decimal number, please ignore the leading zeroes; also if s is an empty string, then it's assumed that the number equals to zero.

Andrew got tired of processing Eugene's requests manually and he asked you to write a program for that. Help him!

輸入

The first line contains string s (1?≤?|s|?≤?105), consisting of digits — the string before processing all the requests.

The second line contains a single integer n (0?≤?n?≤?105) — the number of queries.

The next n lines contain the descriptions of the queries. The i-th query is described by string "di->ti", where di is exactly one digit (from 0 to 9), ti is a string consisting of digits (ti can be an empty string). The sum of lengths of ti for all queries doesn't exceed 105. The queries are written in the order in which they need to be performed.

輸出

Print a single integer — remainder of division of the resulting number by 1000000007 (109?+?7).

樣例

sample input
123123
1
2->00

sample output
10031003

題意

每次選擇選擇一個(gè)數(shù)字,把所有出現(xiàn)這個(gè)數(shù)字的地方替換成一串?dāng)?shù)字。問你求最后這個(gè)數(shù)%10^7的結(jié)果。

題解

如果我們模擬去做,相當(dāng)于是做了一次搜索,每個(gè)出現(xiàn)這個(gè)數(shù)字的地方我們就要執(zhí)行一次替換,而且這個(gè)替換還不一定是最后的結(jié)果,它有可能生出更多的替換(相當(dāng)于是沒有解決的重疊子問題!!!),自然時(shí)間上回承受不了。
但是如果我們采用dp的思想,從下往上做上來(lái),我們解決了一個(gè)子問題,在所有用到這個(gè)子問題的時(shí)候,都只要直接調(diào)用就可以了。
還需要一些位權(quán)展開的知識(shí),腦補(bǔ)一下。

代碼

#include<iostream> #include<cstdio> #include<cstring> #include<map> #include<string> using namespace std;const int maxn = 1e5 + 10; const int mod = 1e9 + 7; typedef __int64 LL; int n;char str[maxn],s[maxn];LL pw[22], val[22];int ql[maxn]; string qr[maxn]; map<char, int> mp; int main() {scanf("%s", &str);scanf("%d", &n);for (int i = 0; i < n; i++) {scanf("%s", s);ql[i] = s[0]-'0';qr[i] = s + 3;}for (int i = 0; i < 10; i++) {pw[i] = 10; val[i] = i;}for (int i = n - 1; i >= 0; i--) {LL sum = 0, tpw = 1;for (int j = 0; j < qr[i].length(); j++) {LL num = qr[i][j] - '0';sum = (sum*pw[num] + val[num]) % mod;tpw = tpw*pw[num] % mod;}val[ql[i]] = sum;pw[ql[i]] = tpw;}LL ans = 0;int len = strlen(str);for (int i = 0; i < len; i++) {int num = str[i] - '0';ans = (ans*pw[num] + val[num]) % mod;}printf("%I64d\n", ans);return 0; }

轉(zhuǎn)載于:https://www.cnblogs.com/fenice/p/5700637.html

總結(jié)

以上是生活随笔為你收集整理的Codeforces Round #265 (Div. 1) C. Substitutes in Number dp的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。