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 题 ...
随机推荐
- C#局域网桌面共享软件制作(二)
链接C#局域网桌面共享软件制作(一) 如果你运行这个软件查看流量监控就会发现1~2M/s左右的上传下载,并且有时会报错“参数无效”,如果你将屏幕截图保存到本地的话每张图片大概4M(bmp).120KB ...
- 显示和隐藏Mac隐藏文件的终端命令
打开终端,输入以下命令: 显示mac隐藏文件的命令: defaults write com.apple.finder AppleShowAllFiles -bool true 隐藏mac隐藏文件的命令 ...
- webservice简介以及简单使用
本文主要是简单的介绍webservice是什么.webservice的由来.webservice的作用,以及后面会介绍webservice的使用. webservice是什么? 目前,Web serv ...
- 无需server-U IIS7.5 在已有的多个WEB网站上配置FTP发布
1 新建一个用于ftp登陆的计算机用户. 操作:开始→管理工具→计算机管理→本地用户和组→用户,新建一个计算机用户,设置好用户名和密码,例如:nenkea nkscl 2 在web站点文件夹下,把新建 ...
- java实现的MySQL自动备份和还原(struts2+Hibernate)---兼容 window+Linux
相信很多朋友都经历过数据库出问题的情况,我也同样(见我的上一篇博文:phpmyadmin误删表后的恢复过程(心惊胆跳啊) ).如果数据很大或者很重要,那么恢复起来是相当困难的,所以我们在做一个相对 ...
- reader,字符流
1. public class Demo1 { public static void main(String[] args) throws IOException { File file = new ...
- 2016/09/21 java split用法
public String[] split(String regex) 默认limit为0 public String[] split(String regex, int limit) 当limit& ...
- [terry笔记]Oracle会话追踪(一):SQL_TRACE&EVENT 10046
SQL_TRACE/10046 事件是 Oracle 提供的用于进行 SQL 跟踪的手段,在日常的数据库问题诊断和解决中是非常常用的方法.但其生成的trace文件需要tkprof工具生成一个可供人 ...
- 编译android程序时DEX过程出现错误
今天编译高级设置时出现了错误,这好坑爹啊~ 于是我开始检查代码,发现代码没有错误啊,然后观察MAKE的步骤才发现是DEX时出现了问题!! 下面是错误的LOG: Information:Using ja ...
- 浅谈Objective-C编译器指令
------<a href="http://www.itheima.com" target="blank">Java培训.Android培训.iOS ...