Codeforces Round #567 (Div. 2) B. Split a Number題解

B. Split a Number

time limit per test

2 seconds

memory limit per test

512 megabytes

input

standard input

output

standard output

Dima worked all day and wrote down on a long paper strip his favorite number nn consisting of ll digits. Unfortunately, the strip turned out to be so long that it didn't fit in the Dima's bookshelf.

To solve the issue, Dima decided to split the strip into two non-empty parts so that each of them contains a positive integer without leading zeros. After that he will compute the sum of the two integers and write it down on a new strip.

Dima wants the resulting integer to be as small as possible, because it increases the chances that the sum will fit it in the bookshelf. Help Dima decide what is the minimum sum he can obtain.

Input

The first line contains a single integer ll (2≤l≤1000002≤l≤100000) — the length of the Dima's favorite number.

The second line contains the positive integer nn initially written on the strip: the Dima's favorite number.

The integer nn consists of exactly ll digits and it does not contain leading zeros. Dima guarantees, that there is at least one valid way to split the strip.

Output

Print a single integer — the smallest number Dima can obtain.

Examples

input

Copy

7
1234567

output

Copy

1801

input

Copy

3
101

output

Copy

11

Note

In the first example Dima can split the number 12345671234567 into integers 12341234 and 567567. Their sum is 18011801.

In the second example Dima can split the number 101101 into integers 1010 and 11. Their sum is 1111. Note that it is impossible to split the strip into "1" and "01" since the numbers can't start with zeros.

 

一下是思路:

思路很簡單,將字符串插入兩個斷點,分別從中間往左和往右尋找第一個符合題目要求的斷點位置,然後手搓一個大數加法

碼農題,思路簡單,實現複雜,寫了進160行代碼

#include<bits/stdc++.h>
using namespace std;
const int maxn = 1e5 + 5;
char str[maxn], str1[maxn], str2[maxn], str3[maxn];
int str1_num, str2_num, str3_num, str_num;
int main()
{
	int n;
	scanf("%d", &n);
	getchar();
	for(int i = 1; i <= n; i++)
	{
		scanf("%c", &str[i]);
	}
	int cutl = n / 2, cutr = n / 2 + 1;
	for(int i = cutl; i >= 0; i--)
	{
		if(str[i + 1] != '0')
		{
			cutl = i;
			break;
		}
	}
	for(int i = cutr; i <= n; i++)
	{
		if(str[i + 1] != '0')
		{
			cutr = i;
			break;
		}
	}
	for(int i = 1; i <= cutl; i++)
	{
		str1[str1_num++] = str[i];
	}
	for(int i = cutl + 1; i <= n; i++)
	{
		str2[str2_num++] = str[i];
	}
	for(int i = 1; i <= cutr; i++)
	{
		str3[str3_num++] = str[i];
	}
	for(int i = cutr + 1; i <= n; i++)
	{
		str[str_num++] = str[i];
		str[str_num] = 0;
	}
	if(str1_num < str2_num)
	{
		int vim = str2_num - str1_num;
		int num = str1_num;
		for(int i = str2_num; i >= vim; i--)
		{
			str1[i] = str1[num--];
		}
		for(int i = 0; i < vim; i++)
		{
			str1[i] = '0';
		}
		str1_num = str2_num;
	}
	if(str_num < str3_num)
	{
		int vim = str3_num - str_num;
		int num = str_num;
		for(int i = str3_num; i >= vim; i--)
		{
			str[i] = str[num--];
		}
		for(int i = 0; i < vim; i++)
		{
			str[i] = '0';
		}
		str_num = str3_num;
	}
 
	//yun suan 
	int div = 0;
	for(int i = str1_num - 1; i >= 0; i--)
	{
		int a = str1[i] - '0', b = str2[i] - '0';
		str1[i] = (a + b + div) % 10 + '0';
		if(a + b + div >= 10)
		{
			div = 1;
		}
		else
			div = 0;
	}
	if(div == 1)
	{
		int num = str1_num - 1;
		for(int i = str1_num; i >= 1; i--)
		{
			str1[i] = str1[num--];
		}
		str1[0] = '1';
		str1_num++;
		div = 0;
	}
	//printf("%s\n%s\n%s\n%s\n", str1, str2, str3, str);
	for(int i = str_num - 1; i >= 0; i--)
	{
		int a = str[i] - '0', b = str3[i] - '0';
		str[i] = (a + b + div) % 10 + '0';
		if(a + b + div >= 10)
		{
			div = 1;
		}
		else
			div = 0;
	}
	if(div == 1)
	{
		int num = str1_num - 1;
		for(int i = str_num; i >= 1; i--)
		{
			str[i] = str[num--];
		}
		str[0] = '1';
		str_num++;
		div = 0;
	}
	if(str_num < str1_num)
	{
		printf("%s\n", str);
	}
	else if(str_num > str1_num)
	{
		printf("%s\n", str1);
	}
	else
	{
		int flag = 0;
		for(int i = 0; i < str_num; i++)
		{
			if(str[i] > str1[i])
			{
				flag = 1;
				break;
			}
			else if(str[i] < str1[i])
			{
				break;
			}
		}
		if(flag)
		{
			printf("%s\n", str1);
		}
		else
			printf("%s\n", str);
	}
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章