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 题 ...
随机推荐
- mysql 导入导出数据库、数据表
Linux下 均在控制台下操作. 导入数据库: 前提:数据库和数据表要存在(已经被创建) (1)将数据表 test_user.sql 导入到test 数据库的test_user 表中 [root@te ...
- Google账户无法登陆-Solved
Author:KillerLegend Date:2014.5.19 From:http://www.cnblogs.com/killerlegend/p/3737888.html 这几天不知道怎么回 ...
- 自定义DatePicker,年月日,不显示其中某几项
经过源码研究:该结构主要包含三个NumberPicker: private final NumberPicker mDayPicker; private final NumberPicker mMon ...
- python 数据类型(sequence 序列、dictionary 词典、动态类型)
文章内容摘自:http://www.cnblogs.com/vamei 1.sequence 序列 sequence(序列)是一组有顺序的元素的集合 (严格的说,是对象的集合,但鉴于我们还没有引入“对 ...
- Python学习教程(learning Python)--1.4 Python数据处理基础
本节主要讨论数据操作及运算符等基础知识,熟悉C语言相关知识的读者请跳过此节. 在高级语言编程过程中,有了数据以后通常要对数据进行相应的数据处理,加.减.乘.除等基本运算,不难理解. 在Python里 ...
- C 封装一个简单二叉树基库
引文 今天分享一个喜欢佩服的伟人,应该算人类文明极大突破者.收藏过一张纸币类型如下 那我们继续科普一段关于他的简介 '高斯有些孤傲,但令人惊奇的是,他春风得意地度过了中产阶级的一生,而 没有遭受到冷 ...
- VBA在WORD中给表格外的字体设置为标题
使用VB可以将表外的字体设置标题字体实际操作如下: VB代码如下: Sub oliver_1() Selection.EndKey Unit:=wdStory '光标移到文末 To ActiveDoc ...
- jdbc 连接 oracle rac
jdbc 连接 oracle rac 的连接串如下: jdbc:oracle:thin:@(DESCRIPTION =(ADDRESS = (PROTOCOL = TCP)(HOST = 192. ...
- 初探oracle删除重复记录,只保留rowid最小的记录
如题,初探oracle删除重复记录,只保留rowid最小的记录(rowid可以反映数据插入到数据库中的顺序) 一.删除重复记录可以使用多种方法,如下只是介绍了两种方法(exist和in两种). 1.首 ...
- nodejs使用mongoose
var mongoose = require("mongoose"); // 连接字符串格式为mongodb://主机/数据库名 mongoose.connect('mongodb ...