题意

开始给出一个长为\(n\)的数字串,有\(m\)次操作按顺序执行,每次把当前数字串中的某一个数码替换成一个数字串\(t\)(可以为空或多位),最后问操作结束后的数字串十进制下模\(10^9+7\)的值。

\(n,m,\sum t\le 10^5\)。

分析

这题很妙啊!

直接做肯定是不行的,因为替换的时候长度没有保证。遇到这种前面对后面有影响的问题,可以考虑反过来做。

如果能够求出所有操作结束后,每个数码代表什么数字串,那么直接代入开始的串就可以得到答案了!

对于每一种数码(0-9)保存到现在为止它代表的数字串模\(10^9+7\)的值,以及它的长度(可以直接用\(10^k\)来表示)。从后往前操作,每一次会改动一种数码,就把改后串用当前的数码状态计算一个当前值出来,代表这个数码当前代表了什么值。

其实就是一个从后往前计算回代的过程。这种from the other end的思路是很奇妙的。

代码

#include<cstdio>
#include<cstring>
#include<string>
#include<utility>
#include<algorithm>
using namespace std;
typedef long long giant;
const int maxn=1e5+10;
const int q=1e9+7;
inline int Plus(int x,int y) {return ((giant)x+(giant)y)%q;}
inline int Multi(int x,int y) {return (giant)x*y%q;}
char s[maxn];
pair<int,string> c[maxn];
pair<int,int> a[10],b[10];
int main() {
#ifndef ONLINE_JUDGE
freopen("test.in","r",stdin);
#endif
int n;
scanf("%s%d",s+1,&n);
for (int i=1;i<=n;++i) {
static char t[maxn];
int d;
scanf("%s",t);
c[i]=make_pair(t[0]-'0',t+3);
}
for (int i=0;i<10;++i) a[i]=make_pair(i,10);
for (int i=n;i;--i) {
int len=c[i].second.length(),d=c[i].first;
int &shu=b[d].first=0,&tmp=b[d].second=1;
if (!len) {
a[d]=b[d];
continue;
}
for (int j=len-1;j>=0;--j) {
int x=c[i].second[j]-'0';
shu=Plus(shu,Multi(tmp,a[x].first));
tmp=Multi(tmp,a[x].second);
}
a[d]=b[d];
}
int m=strlen(s+1),ans=0;
for (int i=m,tmp=1;i;--i) {
int x=s[i]-'0';
ans=Plus(ans,Multi(tmp,a[x].first));
tmp=Multi(tmp,a[x].second);
}
printf("%d\n",ans);
return 0;
}

CF464C-Substitutes in Number的更多相关文章

  1. 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 sec ...

  2. codeforces 464C. Substitutes in Number

    题目链接 C. Substitutes in Number time limit per test 1 second memory limit per test 256 megabytes input ...

  3. Codeforces Round #265 (Div. 2) E. Substitutes in Number

    http://codeforces.com/contest/465/problem/E 给定一个字符串,以及n个变换操作,将一个数字变成一个字符串,可能为空串,然后最后将字符串当成一个数,取模1e9+ ...

  4. findpanel 的相关代码

    https://blog.csdn.net/zengcong2013/article/details/43118189 In addition to this method, you can use ...

  5. JavaScript Math和Number对象

    目录 1. Math 对象:数学对象,提供对数据的数学计算.如:获取绝对值.向上取整等.无构造函数,无法被初始化,只提供静态属性和方法. 2. Number 对象 :Js中提供数字的对象.包含整数.浮 ...

  6. Harmonic Number(调和级数+欧拉常数)

    题意:求f(n)=1/1+1/2+1/3+1/4-1/n   (1 ≤ n ≤ 108).,精确到10-8    (原题在文末) 知识点:      调和级数(即f(n))至今没有一个完全正确的公式, ...

  7. Java 特定规则排序-LeetCode 179 Largest Number

    Given a list of non negative integers, arrange them such that they form the largest number. For exam ...

  8. Eclipse "Unable to install breakpoint due to missing line number attributes..."

    Eclipse 无法找到 该 断点,原因是编译时,字节码改变了,导致eclipse无法读取对应的行了 1.ANT编译的class Eclipse不认,因为eclipse也会编译class.怎么让它们统 ...

  9. 移除HTML5 input在type="number"时的上下小箭头

    /*移除HTML5 input在type="number"时的上下小箭头*/ input::-webkit-outer-spin-button, input::-webkit-in ...

  10. iOS---The maximum number of apps for free development profiles has been reached.

    真机调试免费App ID出现的问题The maximum number of apps for free development profiles has been reached.免费应用程序调试最 ...

随机推荐

  1. linux Ubuntu Kali 安装flash

    http://jingyan.baidu.com/article/fa4125accdeeec28ad709252.html

  2. tkinter事件高级用法实例

    from tkinter import * import threading, time trace = 0 class CanvasEventsDemo: def __init__(self, pa ...

  3. 基于MapReduce的(用户、物品、内容)的协同过滤推荐算法

    1.基于用户的协同过滤推荐算法 利用相似度矩阵*评分矩阵得到推荐列表 已经推荐过的置零 2.基于物品的协同过滤推荐算法 3.基于内容的推荐 算法思想:给用户推荐和他们之前喜欢的物品在内容上相似的物品 ...

  4. JVM知识(上)

    目录 什么是JVM? JVM的生命周期 JVM的体系结构 JVM的数据类型 java虚拟机被称为"虚拟",因为它是一个抽象的计算机定义的规范.要运行一个Java程序,需要一个抽象的 ...

  5. Jmeter接口测试(四)传递参数

    参数设置 Jmeter 支持通过 查询字符串参数(Query String Parameters) 或者 Request body 请求体来传递参数. 1.get请求是普通键值对 get请求一般通过p ...

  6. Appium+python 自动发送邮件(1)(转)

    (原文:https://www.cnblogs.com/fancy0158/p/10056091.html) SMTP:简单传输协议,实在Internet上传输Email的事实标准. Python的s ...

  7. MySQL双主复制

    原文发表于cu:2017-06-12 本文简单介绍MySQL双主复制原理及1个简单是双主复制验证. 一.MySQL双主复制原理 1. 双主复制原理 master-master复制的两台服务器,既是ma ...

  8. 【Shell 开发】Shell 目录

    目录 [第一章]Shell 概述 [第二章]Shell 变量 [第三章]Shell 变量的数值计算 [第四章]Shell 条件测试表达式 [shell 练习1]编写Shell条件句练习 [shell ...

  9. K-近邻算法入门

    K-近邻算法的直观理解就是:给定一个训练集合,对于新的实例,在训练集合中找到k个与该实例最近的邻居,然后根据“少数服从多数”原则判断该实例归属于哪一类,又称“随大流” K-近邻算法的三大要素:K值得选 ...

  10. 第二次ScrumMeeting博客

    第二次ScrumMeeting博客 本次会议于10月26日(四)22时整在3公寓725房间召开,持续15分钟. 与会人员:刘畅.方科栋.辛德泰.窦鑫泽.张安澜. 1. 每个人的工作(有Issue的内容 ...