Codeforces898F - Restoring the Expression(字符串哈希+枚举)

Problem
A correct expression of the form a+b=c was written; a, b 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 10^6.

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并输出,保证答案存在。

解法:
首先想到可以枚举=的位置(记为pos2),此时两个符号会把数字串切割成三个数字(从左往右依次记为a、b、c)。考虑a+b=c,假如c占据了超过一半的数字位,剩下的数字无论怎么分配都不可能比c大,假如c占据了小于1/3的数字位,剩下的数字无论怎么分配都会比c大,所以pos2的位置可以控制在总长度的1/2到2/3之间。因为必定存在答案,所以外层循环i从1/2直接开始枚举,得到答案就输出并跳出循环即可。
在确定了pos2之后就得到了lenc,此时+的位置(记为pos1)也跟着确定了。因为a和b中大的那个要么和c的位数一样多,要么比c的位数少1,通过相加进位达到和c位数一样多,所以只有lena=lenc,lena=lenc-1,lenb=lenc,lenb=lenc-1四种情况。
枚举这四种情况,利用字符串哈希判断a+b=c是否成立。注意此处不能用unsigned long long自然溢出,要自行定义mod,我取了1e9+7。如果不放心hash的结果,可以在hash值相等之后再进行一次大数相加判断是否真的成立。

疑问:
我一开始将mmod赋值为2333,但在样例199100上没有输出结果,试着把其他人的代码中的数改成2333,也没有输出结果,试着把我的mmod改成10,显示Accepted。目前没有想明白是因为什么原因。
判断等式成立的时候使用了(a+b)%mod==c,是十进制计算,基数不设定为10的话进位时会出错。

代码:

#include<iostream>
#include<cstdio>
#include<algorithm>
#include<stdlib.h>
#include<cstring>
using namespace std;

int lens=0,pos1=0,pos2=0;
long long mmod=10,mod=1e9+7;
long long shash[1000000+5],base[1000000+5];
char s[1000000+5];

bool check(){
    long long a=(shash[pos1]-shash[0]*base[pos1]%mod+mod)%mod;
    long long b=(shash[pos2]-shash[pos1]*base[pos2-pos1]%mod+mod)%mod;
    long long c=(shash[lens]-shash[pos2]*base[lens-pos2]%mod+mod)%mod;
    if((a+b)%mod==c%mod)return 1;
    else return 0;
}

void print(){
    for(int i=1;i<=pos1;i++)printf("%c",s[i]);
    printf("+");
    for(int i=pos1+1;i<=pos2;i++)printf("%c",s[i]);
    printf("=");
    for(int i=pos2+1;i<=lens;i++)printf("%c",s[i]);
}

int main() {
    scanf("%s",s+1);
    lens=strlen(s+1);shash[0]=0;base[0]=1;
	for(int i=1;i<=lens;i++){
        shash[i]=(shash[i-1]*mmod%mod+s[i]-'0')%mod;
        base[i]=base[i-1]*mmod%mod;
	}

	for(int i=lens/2;i<lens;i++){
        int len=lens-i;pos2=i;
        if(s[i+1]=='0'&&len>1)continue;

        if(len>0&&(s[len+1]!='0'||i-len==1)&&len<pos2){
            pos1=len;
            if(check()){print();break;}
        }

        if(len>1&&(s[len]!='0'||i-len+1==1)&&len-1<pos2){
            pos1=len-1;
            if(check()){print();break;}
        }

        if(len<i&&(s[i-len+1]!='0'||len==1)){
            pos1=i-len;
            if(check()){print();break;}
        }

        if(len<i+1&&(s[i-len+1+1]!='0'||len-1==1)){
            pos1=i-len+1;
            if(check()){print();break;}
        }
	}
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值