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万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章