codeforces 898F Hash

本文介绍了一种算法,用于解决给定一串数字字符,如何插入加号和等号以形成正确的数学表达式的问题。文章详细解释了算法思路,并通过实例展示了实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

F. Restoring the Expression
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

A correct expression of the form a+b=c was written; ab and c are non-negative integers without leading zeros. In this expression, the plus and equally signs were lost. The task is to restore the expression. In other words, one character '+' and one character '=' should be inserted into given sequence of digits so that:

  • character'+' is placed on the left of character '=',
  • characters '+' and '=' split the sequence into three non-empty subsequences consisting of digits (let's call the left part a, the middle part — b and the right part — c),
  • all the three parts a, b and c do not contain leading zeros,
  • it is true that a+b=c.

It is guaranteed that in given tests answer always exists.

Input

The first line contains a non-empty string consisting of digits. The length of the string does not exceed 106.

Output

Output the restored expression. If there are several solutions, you can print any of them.

Note that the answer at first should contain two terms (divided with symbol '+'), and then the result of their addition, before which symbol'=' should be.

Do not separate numbers and operation signs with spaces. Strictly follow the output format given in the examples.

If you remove symbol '+' and symbol '=' from answer string you should get a string, same as string from the input data.

Examples
input
12345168
output
123+45=168
input
099
output
0+9=9
input
199100
output
1+99=100
input
123123123456456456579579579
output
123123123+456456456=579579579



大意:其实看样例就行了








题解:A+B=C A是x位数,B是y位数,C是z位数, x 和 y 中至少有一个和 z 相等或比 z 小 1。
有了这个条件,就可以O(L)枚举+的位置,然后O(1)找出=可能在的位置,关键就是怎么检验值相等?

RK hash给了我启示,长度为len的序列的前缀1……m的hash值可以用一个MOD进制数表示,想要知道x……y的hash值:
Hash[x,y]=Hash[1,y]-Hash[1,x-1]*10^(y-x+1)
多取几个MOD来检验就基本可以保证正确性。


/*
Welcome Hacking
Wish You High Rating
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<ctime>
#include<cstdlib>
#include<algorithm>
#include<cmath>
#include<string>
using namespace std;
int read(){
    int xx=0,ff=1;char ch=getchar();
    while(ch>'9'||ch<'0'){if(ch=='-')ff=-1;ch=getchar();}
    while(ch>='0'&&ch<='9'){xx=(xx<<3)+(xx<<1)+ch-'0';ch=getchar();}
    return xx*ff;
}
const int maxn=1000010;
char s[maxn];
int len;
int MOD[5]={0,1000000007,19797571,73715923,92431371};
//int MOD[5]={0,1000000,1000000,1000000,1000000};
int Hash[5][maxn];
int mypow(int a,int p,int mod){
	int re=1;
	while(p){
		if(p&1)
			re=1LL*re*a%mod;
		p>>=1;
		a=1LL*a*a%mod;
	}
	return re;
}
int get_Hash(int xx,int yy,int mod){
	//printf("%d %d %d %d %d\n",xx,yy,Hash[mod][yy],Hash[mod][xx-1],mypow(10,yy-xx+1,MOD[mod]));
	return ((Hash[mod][yy]-1LL*Hash[mod][xx-1]*mypow(10,yy-xx+1,MOD[mod]))%MOD[mod]+MOD[mod])%MOD[mod];
}
bool check(int x,int y){
	if(s[1]=='0'&&x!=1)
		return 0;
	if(s[x+1]=='0'&&y!=x+1)
		return 0;
	if(s[y+1]=='0'&&len!=y+1)
		return 0;
	for(int i=1;i<=4;i++)
		if((1LL*get_Hash(1,x,i)+get_Hash(x+1,y,i))%MOD[i]!=get_Hash(y+1,len,i))
			return 0;
	return 1;
}
int main(){
	//freopen("in","r",stdin);
	//freopen("out","w",stdout);
	gets(s+1);
	len=strlen(s+1);
	for(int i=1;i<=4;i++)
		for(int j=1;j<=len;j++)
			Hash[i][j]=(1LL*Hash[i][j-1]*10+s[j]-'0')%MOD[i];
	/*for(int i=1;i<=4;i++){
		for(int j=0;j<=len;j++)
			printf("%d ",Hash[i][j]);
		puts("");
	}*/
	int i,j;
	for(i=1;i<=len/2-(len%2==0);i++){
		j=len-i;
		if(j/2+(j%2==1)>i){
			j=i+(j/2);
			if(check(i,j))
				break;
		}
		else{
			j=len-i;
			if(i==j)
				break;
			if(check(i,j))
				break;
			j=len-i-1;
			if(check(i,j))
				break;
		}
	}
	for(int k=1;k<=len;k++){
		printf("%c",s[k]);
		if(k==i)
			printf("+");
		else if(k==j)
			printf("=");
	}
	puts("");
	return 0;
}

  

细节很多很多,交了五次。

要注意前缀 0 的问题




转载于:https://www.cnblogs.com/lzhAFO/p/8150212.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值