Codeforces Round #265 (Div. 1) C. Substitutes in Number dp
題目鏈接:
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)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 用户空间和内核空间通讯之【proc文件系
- 下一篇: 今天是周一,又是热的天气笼罩。