题目链接:

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

J. Substitutes in Number

time limit per test 1 second
memory 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

题意

每次选择选择一个数字,把所有出现这个数字的地方替换成一串数字。问你求最后这个数%10^7的结果。

题解

如果我们模拟去做,相当于是做了一次搜索,每个出现这个数字的地方我们就要执行一次替换,而且这个替换还不一定是最后的结果,它有可能生出更多的替换(相当于是没有解决的重叠子问题!!!),自然时间上回承受不了。

但是如果我们采用dp的思想,从下往上做上来,我们解决了一个子问题,在所有用到这个子问题的时候,都只要直接调用就可以了。

还需要一些位权展开的知识,脑补一下。

代码

#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;
}

Codeforces Round #265 (Div. 1) C. Substitutes in Number dp的更多相关文章

  1. Codeforces Round #265 (Div. 2) E. Substitutes in Number

    http://codeforces.com/contest/465/problem/E 给定一个字符串,以及n个变换操作,将一个数字变成一个字符串,可能为空串,然后最后将字符串当成一个数,取模1e9+ ...

  2. DP+埃氏筛法 Codeforces Round #304 (Div. 2) D. Soldier and Number Game

    题目传送门 /* 题意:b+1,b+2,...,a 所有数的素数个数和 DP+埃氏筛法:dp[i] 记录i的素数个数和,若i是素数,则为1:否则它可以从一个数乘以素数递推过来 最后改为i之前所有素数个 ...

  3. 数学+DP Codeforces Round #304 (Div. 2) D. Soldier and Number Game

    题目传送门 /* 题意:这题就是求b+1到a的因子个数和. 数学+DP:a[i]保存i的最小因子,dp[i] = dp[i/a[i]] +1;再来一个前缀和 */ /***************** ...

  4. Codeforces Round #367 (Div. 2) C. Hard problem(DP)

    Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...

  5. Codeforces Round #265 (Div. 2) C. No to Palindromes! 构建无回文串子

    http://codeforces.com/contest/465/problem/C 给定n和m,以及一个字符串s,s不存在长度大于2的回文子串,如今要求输出一个字典比s大的字符串,且串中字母在一定 ...

  6. Codeforces Round #265 (Div. 2) D. Restore Cube 立方体判断

    http://codeforces.com/contest/465/problem/D 给定8个点坐标,对于每个点来说,可以随意交换x,y,z坐标的数值.问说8个点是否可以组成立方体. 暴力枚举即可, ...

  7. Codeforces Round #265 (Div. 2) C. No to Palindromes! 构造不含回文子串的串

    http://codeforces.com/contest/465/problem/C 给定n和m,以及一个字符串s,s不存在长度大于2的回文子串,现在要求输出一个字典比s大的字符串,且串中字母在一定 ...

  8. Codeforces Round #265 (Div. 2) D. Restore Cube 立方体推断

    http://codeforces.com/contest/465/problem/D 给定8个点坐标.对于每一个点来说,能够任意交换x.y,z坐标的数值. 问说8个点能否够组成立方体. 暴力枚举就可 ...

  9. Codeforces Round #265 (Div. 2)

    http://codeforces.com/contest/465 rating+7,,简直... 感人肺腑...............蒟蒻就是蒟蒻......... 被虐瞎 a:inc ARG 题 ...

随机推荐

  1. 关于Ajax跨域

    本人因工作需求,编写了一个测试页面,在页面填写完信息之后去向一个站点请求数据,然后返回结果!一开始是直接用Ajax在脚本中去访问,没有大碍(因为目标地址是本机上的一个网站),但是当站点去外部的网站时, ...

  2. .Net 内存泄露

    一.事件引起的内存泄露 1.不手动注销事件也不发生内存泄露的情况 我们经常会写EventHandler += AFunction; 如果没有手动注销这个Event handler类似:EventHan ...

  3. PHP Startup: Unable to load dynamic library

    昨天帮一朋友配置服务器结果发现apache日志中有PHP Warning: PHP Startup: Unable to load dynamic library 提示了,然后调试数据库连接发现提示C ...

  4. php获取客户端浏览器以及操作系统信息的方法

    发布:sunday01   来源:net   阅读: 2   [大 中 小] 在较为智能的程序中,php可以获取客户端浏览器及操作系统信息,然后根据浏览器及系统类型,加载不同的页面,以提供更加个性化的 ...

  5. xtrabackup之Innobackupex全备恢复

    一.当前环境 [mysql@hadoop1 ~]$ mysql --defaults-/my.cnf -uroot -p123456 -P3306 mysql> show variables l ...

  6. Scrapy Learning笔记(四)- Scrapy双向爬取

    摘要:介绍了使用Scrapy进行双向爬取(对付分类信息网站)的方法. 所谓的双向爬取是指以下这种情况,我要对某个生活分类信息的网站进行数据爬取,譬如要爬取租房信息栏目,我在该栏目的索引页看到如下页面, ...

  7. STM32F0xx_TIM基本延时配置详细过程

    前言 关于定时器大家都应该不会陌生,因为处理器都有这个功能.今天总结的F0系列芯片的定时器根据芯片型号不同,数量也不同.定时器分类:基本定时器.通用定时器和高级定时器.计数位数也有不同,有16位的,有 ...

  8. highcharts 显示点值的效果

    plotOptions: { line: { /* <s:if test='#request.rdflags=="point"'> <s:if test=&quo ...

  9. VBA在WORD中给表格外的字体设置为标题

    使用VB可以将表外的字体设置标题字体实际操作如下: VB代码如下: Sub oliver_1() Selection.EndKey Unit:=wdStory '光标移到文末 To ActiveDoc ...

  10. vnextcn

    Flag 标题 通过 提交 AC% 难度   E1 编写Hello World网站 9 17 52% 1   E2 编写Hello World控制台程序 11 13 84% 1   E3 控制器基础练 ...