CodeForces 898F (2020.3.8训练H题)

Restoring the Expression

题目地址:http://codeforces.com/problemset/problem/898/F

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 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.

题意:已知一个顺序排好的数字字符串,每位都是十进制,要求给原串进行分割,加入‘+’和‘=’,使字符串成为"a+b=c"的形式。

理解:使用哈希表,任意一个子串都映射成一个数字,然后就是怎样去凑a+b=c的形式,无非就是四种情况。

  1. len_a = len_c
  2. len_a + 1 = len_c
  3. len_b = len_c
  4. len_b + 1 = len_c

然后再写个check函数对每个函数检验是否成立就好了。

ac代码如下

#include<iostream>
#include<string>
using namespace std;

#define mod 1000000007
#define ll long long 
#define maxn 1000005
char s[maxn];
ll bat = 10;
ll hashh[maxn];
int len;
 
ll Qpow(ll a, ll b, ll p) //快速幂
{
    ll ans = 1;
    while (b > 0)
    {
        if (b & 1) ans = (ans * a) % p;
        a = (a * a) % p;
        b >>= 1;
    }
    return ans % p;
}

ll gethash(ll l, ll r)
{
	ll ans = (hashh[r] - hashh[l - 1] * Qpow(bat, r - l + 1, mod)) % mod;
	if (ans < 0) ans += mod; //可能会出现小于零的情况
	return ans;
}

int output(int al, int bl) //已知两个加数长度,输出结果
{
	for (int i = 1; i <= al; i++)
	{
		cout << s[i];
	}
	cout << "+";
	for (int i = al + 1; i <= al + bl; i++)
	{
		cout << s[i];
	}
	cout << "=";
	for (int i = al + bl + 1; i <= len; i++)
	{
		cout << s[i];
	}
	cout << endl;
	return 1;
}

int check(int al, int bl)//检验长度为al和bl的相加情况是否合理
{
	if (al <= 0 || bl <= 0 || len - al - bl < al || len - al - bl < bl || len - al - bl <= 0 || len - al - bl <= 0)
		return 0;
	if (s[1] == '0' && al != 1)
		return 0;
	if (s[al + 1] == '0' && bl != 1)
		return 0;
	if (s[al + bl + 1] == '0' && len - al - bl != 1)
		return 0;
	ll a = gethash(1, al);
	ll b = gethash(al + 1, al + bl);
	ll c = gethash(al + bl + 1, len);//通过gethash函数获取两个加数和第三数大小
	if ((a + b) % mod == c)//检验是否相等,记得对mod取余
	{
		output(al, bl);
		return 1;
	}
	return 0;
}

int main()
{
	scanf("%s", s + 1);
	len = strlen(s + 1);//输入字符串从下标1存到n
	hashh[0] = 0;
	for (int i = 1; i <= len; i++)
	{
		hashh[i]=(hashh[i - 1] * bat + s[i] - '0') % mod;//计算每个位置的哈希值
	}
	for (int i = 1; i <= len / 2 + 1; i++)
	{
		if (check(i, len - 2 * i))//len(a) = len(c) 
			break;      
        if (check(i, len - 2 * i - 1)) //len(a) + 1 = len(c)
			break;    
        if (check(len - 2 * i, i)) //len(b)=len(c)
			break;      
        if (check(len - 2 * i - 1, i)) //len(b) + 1=len(c)
			break;
	}
	return 0;
}
  • 4
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值