题目链接:

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. 转:浅谈关于b、h标签的优化技巧

    <b>标签优化 <b>标签是一种加粗标记,作用就是加粗文章中的关键词,对于文章中重要的关键词加粗起到有利于用户阅读的作用.<b>标签的使用对于优化的作用是非常大的, ...

  2. rest api设计[资源]

    web开发资源列表 http://www.bentobox.io/ rest api资源 Designing an API http://www.vinaysahni.com/best-practic ...

  3. MySQL5.7重置root密码

    版本更新 缘故,好多网上的教程都不适用了,甚至连官网的文档也不是能够顺利操作的. 如果 MySQL 正在运行,首先杀之: killall -TERM mysqld. 运行mysqld_safe --s ...

  4. js实现touch移动触屏滑动事件

    在开始描述touch事件之前,需要先描述一下多触式系统中特有的touch对象(android和iOS乃至nokia最新的meego系统都模拟了类 似的对象).这个对象封装一次屏幕触摸,一般来自于手指. ...

  5. Timer wheel etc.

    http://code.google.com/p/hierarchal-wheel-timer/ 最小堆的实现(C 语言版) 最小堆的实现(java) Linux 下定时器的实现方式分析 更快bobh ...

  6. 记录C++学习历程

    从今天开始学习C++,将学习中遇到的问题,以及解决方案记录在这个博客里. 函数 1.C++函数声明(原型) 函数原型跟函数的定义在返回值类型,函数名,参数上必须完全一致. 2.程序的内存区域:全局数据 ...

  7. IOS应用程序生命周期

    一.IOS应用的5种状态 Not Running(非运行状态) 应用没有运行或被系统终止. Inactive(前台非活动状态) 应用正在进入前台状态,但是还不能接受事件处理. Active(前台活动状 ...

  8. .NET String.Format 方法 线程安全问题

    碰到这个问题 是在和淘宝做信息交互的时候, 接收别人N年前的代码. 代码逻辑很简单,就是取得信息 数据库查询  响应请求返回结果. 最近淘宝的人反映说 N多账户使用的是一个单号.理论上来说 是应该每次 ...

  9. activiti

    http://activiti.org/designer/archived/  eclipse plugin

  10. Oracle 错误代码

    ORA-00001: 违反唯一约束条件 (.) ORA-00017: 请求会话以设置跟踪事件 ORA-00018: 超出最大会话数 ORA-00019: 超出最大会话许可数 ORA-00020: 超出 ...