程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> C++入門知識 >> Codeforces 464 C. Substitutes in Number 動態規劃法題解

Codeforces 464 C. Substitutes in Number 動態規劃法題解

編輯:C++入門知識

Codeforces 464 C. Substitutes in Number 動態規劃法題解


C. Substitutes in Number time limit per test 1 second memory limit per test 256 megabytes input standard input output standard output

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!

Input

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 tifor all queries doesn't exceed 105. The queries are written in the order in which they need to be performed.

Output

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

Sample test(s) input
123123
1
2->00
output
10031003
input
123123
1
3->
output
1212
input
222
2
2->0
0->7
output
777
input
1000000008
0
output
1
Note

Note that the leading zeroes are not removed from string s after the replacement (you can see it in the third sample).


本題使用動態規劃法思想。

因為需要一步一步地替換相對應的數字的,如果直接模擬,那麼就需要大量插入和刪除操作,最快也需要lg(n)的效率,但是最後數列就會變得非常長,這樣最後計算結果遍歷一次也會超時的。故此使用數據結構加速替換操作,並不是好辦法。

這就使用動態規劃法從後往前替換,相當於路徑壓縮了,一步直接把數字替換成最終結果的數字。

也要記錄好每個數字最終替換成多少個數位,以便正確計算結果。

可以畫樹來模擬一下替換操作,那麼從葉子節點往根節點替換數字,把所有的路徑都直接壓縮起來。


#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
using namespace std;

const int MAX_N = (int)1E5 + 1;
const int MOD = 1000000007;
char s[MAX_N];
char query[MAX_N];
string qnum[MAX_N];
short qid[MAX_N];

int replacement[10], tenpows[10], N;

int main()
{
	gets(s);
	scanf("%d\n", &N);
	for (int i = 0; i < N; i++)
	{
		scanf("%s", query);//gets(query)每次用gets都會出現莫名其妙的錯誤!
		qid[i] = query[0] - '0';
		qnum[i] = query+3;
	}
	for (int i = 0; i < 10; i++)
	{
		replacement[i] = i;
		tenpows[i] = 10;
	}
	for (int i = N-1; i >= 0; i--)
	{
		long long rm = 0LL, tpow = 1LL;
		for (size_t j = 0; j < qnum[i].size(); j++)
		{
			
			int num = qnum[i][j] - '0';
			tpow = tpow * tenpows[num] % MOD;
			rm = (rm*tenpows[num]%MOD + replacement[num]) % MOD;
		}
		replacement[qid[i]] = (int)rm;
		tenpows[qid[i]] = (int)tpow;
	}
	long long r = 0LL;
	for (char *p = s; *p; p++) r = (r*tenpows[*p-'0'] + replacement[*p-'0'])%MOD;
	printf("%I64d", r);
	return 0;
}



  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved