codeforces 464C Substitutes in Number

题意:给定一个数字串。有m次操作,形式如a->b,那么可以把字符串中出现的a替换成b。如果b为空串那么相当于将a删去。问最后

的数字串的值对10^9+7的模值。

思路:学习了九野的题解。我们将所有操作存入结构体。用x数组记录字符串某位为i所代表的值(已经对10^9+7取模),用w数组记

录这个数位在数字串转换成数值的时候需要乘以的10的次方。我们最后一个操作向前推直到最终确定每个数字串数位的x和w。最后

算出转换成数值的ans对10^9+7的模值。详见代码:

// file name: codeforces464C.cpp //
// author: kereo //
// create time:  2014年09月08日 星期一 14时59分06秒 //
//***********************************//
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include<set>
#include<map>
#include<vector>
#include<stack>
#include<cmath>
#include<string>
#include<algorithm>
using namespace std;
typedef long long ll;
const int MAXN=100000+100;
const int inf=0x3fffffff;
const int mod=1000000000+7;
#define L(x) (x<<1)
#define R(x) (x<<1|1)
int n;
char str[MAXN],t[MAXN];
ll x[10],w[10]; //x记录字符串str中数字i对应的实际数,w记录对应实际数的位数
struct node
{
	int id;
	string s;
}p[MAXN];
void init(){
	for(int i=0;i<10;i++)
		x[i]=i,w[i]=10;
}
int main()
{
	while(~scanf("%s",str)){
		scanf("%d",&n);
		for(int i=1;i<=n;i++){
			scanf("%d->",&p[i].id);
			gets(t); p[i].s=t;
		}
		init();
		for(int i=n;i>=1;i--){
			ll len=p[i].s.length();
			if(len == 0){
				x[p[i].id]=0; w[p[i].id]=1;
				continue;
			}
			ll ans1=0,ans2=1; //ans1记录数,ans2记录位数
			for(ll j=0;j<len;j++){
				ans1*=w[p[i].s[j]-'0'];
				ans1+=x[p[i].s[j]-'0'];
				if(ans1>=mod)
					ans1%=mod;
				ans2*=w[p[i].s[j]-'0'];
				if(ans2>=mod)
					ans2%=mod;
			}
			x[p[i].id]=ans1;
			w[p[i].id]=ans2;
		}
		ll ans=0;
		int len=strlen(str);
		for(int i=0;i<len;i++){
			ans*=w[str[i]-'0'];
			ans+=x[str[i]-'0'];
			if(ans>=mod)
				ans%=mod;
		}
		cout<<ans<<endl;
	}
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值