CF464C-Substitutes in Number
题意
开始给出一个长为\(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的更多相关文章
- 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 ...
- codeforces 464C. Substitutes in Number
题目链接 C. Substitutes in Number time limit per test 1 second memory limit per test 256 megabytes input ...
- Codeforces Round #265 (Div. 2) E. Substitutes in Number
http://codeforces.com/contest/465/problem/E 给定一个字符串,以及n个变换操作,将一个数字变成一个字符串,可能为空串,然后最后将字符串当成一个数,取模1e9+ ...
- findpanel 的相关代码
https://blog.csdn.net/zengcong2013/article/details/43118189 In addition to this method, you can use ...
- JavaScript Math和Number对象
目录 1. Math 对象:数学对象,提供对数据的数学计算.如:获取绝对值.向上取整等.无构造函数,无法被初始化,只提供静态属性和方法. 2. Number 对象 :Js中提供数字的对象.包含整数.浮 ...
- Harmonic Number(调和级数+欧拉常数)
题意:求f(n)=1/1+1/2+1/3+1/4-1/n (1 ≤ n ≤ 108).,精确到10-8 (原题在文末) 知识点: 调和级数(即f(n))至今没有一个完全正确的公式, ...
- Java 特定规则排序-LeetCode 179 Largest Number
Given a list of non negative integers, arrange them such that they form the largest number. For exam ...
- Eclipse "Unable to install breakpoint due to missing line number attributes..."
Eclipse 无法找到 该 断点,原因是编译时,字节码改变了,导致eclipse无法读取对应的行了 1.ANT编译的class Eclipse不认,因为eclipse也会编译class.怎么让它们统 ...
- 移除HTML5 input在type="number"时的上下小箭头
/*移除HTML5 input在type="number"时的上下小箭头*/ input::-webkit-outer-spin-button, input::-webkit-in ...
- 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.免费应用程序调试最 ...
随机推荐
- 小程序if else 判断显示隐藏
wxml: <view> <text wx:if="{{ifnumber>80}}">{{ifnumber}}</text> <te ...
- jdk和tomcat版本对应
见tomcat的官网说明:tomcat.apache.org/whichversion.html Apache Tomcat ®是一个开源软件实现了Java Servlet和JavaServer Pa ...
- C# 调用C++ dll 返回char*调用方式(StringBuilder乱码)
// CDLLDemo.cpp : 定义 DLL 应用程序的导出函数. // #include "stdafx.h" #include "string.h" # ...
- SQL Server 2008 R2 链接 Oracle 10g
首先sqlserver 链接oracle可以通过两个访问接口: “MSDAORA” 和“OraOLEDB.Oracle” 1.“MSDAORA”访问接口是由Microsoft OLE DB Provi ...
- info信息的获取
一.绝对路径(_SERVER[“SCRIPT_FILENAME”])这个是最常用,也是最有效的一个办法,找到phpinfo()页面可以直接找到网站的绝对路径,对于写shell和信息搜集是必不可少的.二 ...
- linux源码学习-for_each_cpu
刚开始读linux源码,第一眼就看到了这个很有意思的函数族,周末好好研究一下 3.13 这个组都是宏定义for循环,分析的时候注意到cpumask_next(),它在一个文件中定义了两次,还不是重载, ...
- RC电路简介,RC串并联电路的工作原理及应用
RC电路简介,RC串并联电路的工作原理及应用 RC电路全称Resistance-Capacitance Circuits.一个 相移电路(RC电路)或称 RC滤波器. RC网络, 是一个包含利用电压源 ...
- Method 'ExecuteAsync' in type 'System.Data.Entity.SqlServer.DefaultSqlExecutionStrategy' does not have an implementation
一.错误信息 Entity Framework 6.0数据迁移:Add-Migration XXXX 命令发生错误 System.Reflection.TargetInvocationExceptio ...
- mysql先删除后插入导致死锁
所报的错误为:pymysql.err.OperationalError: (1213, 'Deadlock found when trying to get lock; try restarting ...
- scrum立会报告+燃尽图(第二周第五次)
此作业要求参见:https://edu.cnblogs.com/campus/nenu/2018fall/homework/2250 一.小组介绍 组名:杨老师粉丝群 组长:乔静玉 组员:吴奕瑶.公冶 ...