题意

开始给出一个长为\(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. 20155327李百乾 Exp3 免杀原理与实践

    20155327李百乾 Exp3 免杀原理与实践 实践guocheng 一.Msfvenom使用编码器 1.利用(virustota)[https://www.virustotal.com/]检测实验 ...

  2. 20155330 2016-2017-2《Java程序设计》课程总结

    20155330 2016-2017-2<Java程序设计>课程总结 目录 每周作业链接汇总 实验报告链接汇总 代码托管 课堂项目实践 课程收获与不足 问卷调查 给出你的总结中涉及到的链接 ...

  3. Using Xpath With Default XML Namespace in C#

    If you have a XML file without any prefix in the namespace: <bookstore xmlns="http://www.con ...

  4. Spring SimpleJdbcOperations 批量更新

    1.控制台代码 import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowM ...

  5. 【LOJ10121】与众不同

    [LOJ10121]与众不同 题面 LOJ 题解 这题是_\(tham\)给\(ztl\)他们做的,然而这道题™居然还想了蛮久... 首先可以尺取出一个位置\(i\)上一个合法的最远位置\(pre_i ...

  6. 4540: [Hnoi2016]序列

    4540: [Hnoi2016]序列 https://www.lydsy.com/JudgeOnline/problem.php?id=4540 分析: 莫队+RMQ+单调栈. 考虑加入一个点后,区间 ...

  7. equals和==方法比较(三)--Long中LongCache源码分析

    下面我们来分析,上篇博客中遗留的问题,为什么下方的两个一个是true,两一个是false那? //true Long l1=123l; Long l2=123l; System.out.println ...

  8. 关于Python的装饰器(2)

    Python中被装饰器修饰的函数,解析后会生成一个参数是被修饰函数的装饰器函数对象,可以调用,可以接受传参(如果被修饰的函数定义了参数),实际调用的时候,尽管代码里值写了被修饰的函数,被调用的却是最终 ...

  9. 自己通过Cygwin编译的windowsx86下的更新至4.0.1

    采用方法:https://my.oschina.net/maxid/blog/186506 方法中在3.2.6未找到src/redis.h文件 未修改 方法中 /deps/hiredis/net.c ...

  10. Hyperledger Fabric 1.0 从零开始(十三)——orderer分布式方案

    简述 在搭建HyperLedger Fabric环境的过程中,我们会用到一个configtx.yaml文件(可参考Hyperledger Fabric 1.0 从零开始(八)——Fabric多节点集群 ...