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
题意
每次选择选择一个数字,把所有出现这个数字的地方替换成一串数字。问你求最后这个数%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的更多相关文章
- Codeforces Round #265 (Div. 2) E. Substitutes in Number
http://codeforces.com/contest/465/problem/E 给定一个字符串,以及n个变换操作,将一个数字变成一个字符串,可能为空串,然后最后将字符串当成一个数,取模1e9+ ...
- DP+埃氏筛法 Codeforces Round #304 (Div. 2) D. Soldier and Number Game
题目传送门 /* 题意:b+1,b+2,...,a 所有数的素数个数和 DP+埃氏筛法:dp[i] 记录i的素数个数和,若i是素数,则为1:否则它可以从一个数乘以素数递推过来 最后改为i之前所有素数个 ...
- 数学+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;再来一个前缀和 */ /***************** ...
- Codeforces Round #367 (Div. 2) C. Hard problem(DP)
Hard problem 题目链接: http://codeforces.com/contest/706/problem/C Description Vasiliy is fond of solvin ...
- Codeforces Round #265 (Div. 2) C. No to Palindromes! 构建无回文串子
http://codeforces.com/contest/465/problem/C 给定n和m,以及一个字符串s,s不存在长度大于2的回文子串,如今要求输出一个字典比s大的字符串,且串中字母在一定 ...
- Codeforces Round #265 (Div. 2) D. Restore Cube 立方体判断
http://codeforces.com/contest/465/problem/D 给定8个点坐标,对于每个点来说,可以随意交换x,y,z坐标的数值.问说8个点是否可以组成立方体. 暴力枚举即可, ...
- Codeforces Round #265 (Div. 2) C. No to Palindromes! 构造不含回文子串的串
http://codeforces.com/contest/465/problem/C 给定n和m,以及一个字符串s,s不存在长度大于2的回文子串,现在要求输出一个字典比s大的字符串,且串中字母在一定 ...
- Codeforces Round #265 (Div. 2) D. Restore Cube 立方体推断
http://codeforces.com/contest/465/problem/D 给定8个点坐标.对于每一个点来说,能够任意交换x.y,z坐标的数值. 问说8个点能否够组成立方体. 暴力枚举就可 ...
- Codeforces Round #265 (Div. 2)
http://codeforces.com/contest/465 rating+7,,简直... 感人肺腑...............蒟蒻就是蒟蒻......... 被虐瞎 a:inc ARG 题 ...
随机推荐
- Sublime Text 2 常用快捷键
Sublime Text 2 常用的快捷键 (不包含插件快捷键) Ctrl+P 打开文件搜索框,可以直接输入文件名搜索,或者输入@funcName 可以直接到函数定义处,输入#key 可以直接查找,输 ...
- c语言学习的第6天
#include <stdio.h> int main() { int x=100; if(x==0) { printf("x等于0\n"); printf(" ...
- php中empty(), is_null(), isset()函数区别
empty(), is_null(), isset()真值表(区别) 我们先来看看这3个函数的功能描述 www.111cn.net isset 判断变量是否已存在,如果变量存在则返回 TRUE,否则返 ...
- Flex 4.0及4.6发布的网络应用在内网内会访问很慢的解决方案
Flex 4.x 开发的程序部署在外网在能访问到www.adobe.com的时能够很快加载完成,但是部署在本地局域网,不能访问外网的服务器上,用浏览器访问应用需要加载几分钟的时间,这种等待时间客户几乎 ...
- sql 子查询要命名
Date1 from ( select distinct Date1 from TableName where Date1 > '2013-5-1' )A --这里加个A,B,C随便你 或者as ...
- xtrabackup之Innobackupex增量备份及恢复
演示增量备份 #启动一个全备 innobackupex \ > --defaults-/my.cnf \ > --host=127.0.0.1 \ > --user=xtrabk \ ...
- afddaf
//import javax.swing.*; import javax.swing.JFrame; import javax.swing.JButton; import javax.swing.JL ...
- 数据结构中的堆棧在C#中的实现
一.大致学习 堆棧是一种面向表的数据结构,堆棧中的数据只能在标的某一短进行添加和删除操作,是一种典型的(LIFO)数据结构. 现实生活中的理解:自助餐厅的盘子堆,人们总是从顶部取走盘子,当洗碗工把洗好 ...
- poj 2153 Rank List
原题链接:http://poj.org/problem?id=2153 简单题,map,平衡树均可.. map: #include<algorithm> #include<iostr ...
- angularjs+nodejs+mongodb三件套
说实话,自己对于web前段的认识并不是太深入,但是因为项目的需要,所以有的时候肯定会需要接触到web前段的知识点.说到web前端想必大家肯定会想到css+js+html,的确web前端的工作,从某总角 ...