Codeforces 464C 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 ti for 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).



DIV2的第五題 DIV1的第三題,大致意思就是最多100000個操作使得一個0-9的數字變成一串數字,初始有個長度最長爲100000的數字串,問我們最終這個數字串所代表的數字對1e9+7取餘得到的是多少。

題目中有一句話說前綴0不用輸出,這句話提醒我們這題是純數學題。

這組樣例:

123123

1

2->00

答案爲10031003,2變成了00之外其他不變,我們要的只是10031003而已,那就想到每一個數字對最終答案的貢獻值之和必爲10031003,每個數字的貢獻值等於自己將變成的數字串*這個數字串所在位數,如123123最右邊的3貢獻值爲3*1最右邊的2爲0*10,由於只有加法和乘法,滿足同餘,故中間過程均可取餘。每個數字將變成的數字串可以從後往前得出,記得存下數字串的位數,我們要做的還有預處理出10的0-100000次方對1e9+7取餘。


代碼:

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<ctime>
#include<string>
#include<cstring>
#include<algorithm>
#include<fstream>
#include<queue>
#include<stack> 
#include<vector>
#include<cmath>
#include<iomanip>
#define rep(i,n) for(i=1;i<=n;i++)
#define MM(a,t) memset(a,t,sizeof(a))
#define INF 1e9
typedef long long ll;
#define mdu 1000000007
using namespace std;
struct node{
  ll v,wei;	
}yu[20]; 
ll weiyu[100020];
struct node2{
	int v,l,r;
	ll wei;
}qu[100020];
char st[100020],st2[100020];
int n,l,lst;
void setup(){
	int i,j;
	
	weiyu[0]=1;
	rep(i,100001) weiyu[i]=weiyu[i-1]*10%mdu;
	for(i=0;i<=9;i++){
		yu[i].v=i;
		yu[i].wei=10;
	}
	l=0; lst=strlen(st);

}
int main()
{
	int i,j;

    while(scanf("%s",st)!=EOF){
      setup();
	  scanf("%d",&n);
	  rep(i,n){
  	    char ss[100100];
		scanf("%s",ss);
		qu[i].v=int(ss[0]-'0'); qu[i].wei=strlen(ss)-3;
		for(j=l;j<l+qu[i].wei;j++)
		  st2[j]=ss[j-l+3];
		qu[i].l=l; l+=qu[i].wei; qu[i].r=l-1;   
  	  }
	  for(i=n;i>=1;i--){
  	    int l=qu[i].l,r=qu[i].r;
  	    ll wei=qu[i].wei,zv=0,zwei=1;
		for(j=r;j>=l;j--){
		  int tp=int(st2[j]-'0');
		  zv=(zv+yu[tp].v*zwei%mdu)%mdu;
		  zwei=zwei*yu[tp].wei%mdu;	
		}	
		yu[qu[i].v].v=zv;
		yu[qu[i].v].wei=zwei;
  	  }
  	  ll zv=0,zwei=1;
  	  for(i=lst-1;i>=0;i--){
 		  int tp=int(st[i]-'0');
		  zv=(zv+yu[tp].v*zwei%mdu)%mdu;
		  zwei=zwei*yu[tp].wei%mdu;	
  	  }
  	  printf("%lld\n",zv);
    }
    
	
	return 0;
}




發佈了41 篇原創文章 · 獲贊 1 · 訪問量 2萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章