Codeforces 464 C. Substitutes in Number 动态规划法题解

C. Substitutes in Number

time limit per test

1 second

memory limit per test

256 megabytes

input

standard input

output

standard output

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!

Input

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 tifor
all queries doesn‘t exceed 105.
The queries are written in the order in which they need to be performed.

Output

Print a single integer — remainder of division of the resulting number by 1000000007 (109?+?7).

Sample test(s)

input

123123
1
2->00

output

10031003

input

123123
1
3->

output

1212

input

222
2
2->0
0->7

output

777

input

1000000008
0

output

1

Note

Note that the leading zeroes are not removed from string s after the replacement (you can see it in the third sample).

本题使用动态规划法思想。

因为需要一步一步地替换相对应的数字的,如果直接模拟,那么就需要大量插入和删除操作,最快也需要lg(n)的效率,但是最后数列就会变得非常长,这样最后计算结果遍历一次也会超时的。故此使用数据结构加速替换操作,并不是好办法。

这就使用动态规划法从后往前替换,相当于路径压缩了,一步直接把数字替换成最终结果的数字。

也要记录好每个数字最终替换成多少个数位,以便正确计算结果。

可以画树来模拟一下替换操作,那么从叶子节点往根节点替换数字,把所有的路径都直接压缩起来。

#include <stdio.h>
#include <stdlib.h>
#include <vector>
#include <string.h>
#include <algorithm>
#include <iostream>
#include <string>
#include <limits.h>
#include <stack>
#include <queue>
#include <set>
#include <map>
using namespace std;

const int MAX_N = (int)1E5 + 1;
const int MOD = 1000000007;
char s[MAX_N];
char query[MAX_N];
string qnum[MAX_N];
short qid[MAX_N];

int replacement[10], tenpows[10], N;

int main()
{
	gets(s);
	scanf("%d\n", &N);
	for (int i = 0; i < N; i++)
	{
		scanf("%s", query);//gets(query)每次用gets都会出现莫名其妙的错误!
		qid[i] = query[0] - '0';
		qnum[i] = query+3;
	}
	for (int i = 0; i < 10; i++)
	{
		replacement[i] = i;
		tenpows[i] = 10;
	}
	for (int i = N-1; i >= 0; i--)
	{
		long long rm = 0LL, tpow = 1LL;
		for (size_t j = 0; j < qnum[i].size(); j++)
		{

			int num = qnum[i][j] - '0';
			tpow = tpow * tenpows[num] % MOD;
			rm = (rm*tenpows[num]%MOD + replacement[num]) % MOD;
		}
		replacement[qid[i]] = (int)rm;
		tenpows[qid[i]] = (int)tpow;
	}
	long long r = 0LL;
	for (char *p = s; *p; p++) r = (r*tenpows[*p-'0'] + replacement[*p-'0'])%MOD;
	printf("%I64d", r);
	return 0;
}
时间: 2024-10-29 04:04:49

Codeforces 464 C. Substitutes in Number 动态规划法题解的相关文章

Codeforces 464C Substitutes in Number(高效+快速幂)

题目链接:Codeforces 464C Substitutes in Number 题目大意:给定一个字符串,以及n中变换操作,将一个数字变成一个字符串,可能为空串,然后最后将字符串当成一 个数,取模1e9+7. 解题思路:将操作倒过来处理,这样维护每个数来的val,len两个,val表示对应数值取模1e9+7,len表示对应有多少 位,再计算的过程中要使用. #include <cstdio> #include <cstring> #include <vector>

POJ 1019 Number Sequence 题解

这又是一道看似简单,实际挺困难的题目. 本来想做道基础题消遣一下的,没想到反被消遣了-_-|||. 看个人的基础吧,对于数学好的会简单点,但是由于情况太多,需要都考虑全,故此难度应该在4星以上了. 我这里使用的方法就是直接打表,然后直接模拟,利用打表去掉一大段数据,剩下数据量十分小了,故此可以直接模拟. 打表是为了计算前面的周期数,把周期数直接去掉. 主要难点是后面10位数以上的数有2位, 3位,4位等情况要考虑.- 下面使用getNewNums一个函数解决了,想通了,就几行代码,还不用难理解的

HDU 1018 Big Number 数学题解

Problem Description In many applications very large integers numbers are required. Some of these applications are using keys for secure transmission of data, encryption, etc. In this problem you are given a number, you have to determine the number of

CodeForces 1325E - Ehab&#39;s REAL Number Theory Problem【质因子+】

题意: ??给定一个数组 \(a\) ,数组中任意一个元素的因子数不超过 \(7\) ,找出一个最短的子序列,满足该子序列之积为完全平方数.输出其长度. 数据范围:\(1≤n≤10^5,1≤a_i≤10^6\) 分析: ??首先,对于数组中的每个元素,如果其因子中包含有一个完全平方数,那么可以把该完全平方数除去,不影响最后的结果. ??然后,可以发现,当一个数的因子个数 \(\leq 7\) 时,其包含的质因子个数 \(\leq 2\).(如果有3个质因子,那么至少有 \(8\) 个因子)当我们

Codeforces 464C Substitutes in Number 同余定理+模拟

题目链接:点击打开链接 题意: 给定一串数字 下面有n个操作 每行格式形如 d->t d为一位数字,t为任意长度的数字. t的长度和不超过100000 问:最后的结果%1e9+7 思路: 首先我们可以得到一个结论: 同余定理使用后不能再修改数字. 那么为了让同余定理能够使用,我们倒序处理每个数字,这样就能保证能够使用同余定理. 记录每个数字实际代表的数字和实际对应的位数. 然后倒序处理上来即可. #include <stdio.h> #include <string.h> #

Codeforces 821A Okabe and Future Gadget Laboratory 题解

此文为博主原创题解,转载时请通知博主,并把原文链接放在正文醒目位置. 题目链接:http://codeforces.com/problemset/problem/821/A 时间限制:2秒 空间限制:256M Okabe要改进他的实验室.实验室用一个n*n的正方形网格表示(n为正整数).他认为,一个“好实验室”的网格内每一个不等于1的数字都可以用同一行和同一列的某个数字之和表示.换句话说,对于任意x,y(1 ≤ x, y ≤ n 且 ax, y ≠ 1,),存在两个数s和t,使得ax, y = 

Codeforces Round #198 (Div. 2)A,B题解

Codeforces Round #198 (Div. 2) 昨天看到奋斗群的群赛,好奇的去做了一下, 大概花了3个小时Ak,我大概可以退役了吧 那下面来稍微总结一下 A. The Wall Iahub and his friend Floyd have started painting a wall. Iahub is painting the wall red and Floyd is painting it pink. You can consider the wall being mad

POJ 3616 Milking Time 动态规划法题解

Description Bessie is such a hard-working cow. In fact, she is so focused on maximizing her productivity that she decides to schedule her next N (1 ≤N ≤ 1,000,000) hours (conveniently labeled 0..N-1) so that she produces as much milk as possible. Far

Codeforces Round #246 (Div. 2) (ABCD详细题解)

比赛链接:http://codeforces.com/contest/432 A. Choosing Teams time limit per test:1 second memory limit per test:256 megabytes The Saratov State University Olympiad Programmers Training Center (SSU OPTC) has n students. For each student you know the numbe