Description
Vasya plays Robot Bicorn Attack.
The game consists of three rounds. For each one a non-negative integer amount of points is given. The result of the game is the sum of obtained points. Vasya has already played three rounds and wrote obtained points one by one (without leading zeros) into the string s. Vasya decided to brag about his achievement to the friends. However, he has forgotten how many points he got for each round. The only thing he remembers is the strings.
Help Vasya to find out what is the maximum amount of points he could get. Take into account that Vasya played Robot Bicorn Attack for the first time, so he could not get more than 1000000 (106) points for one round.
Input
The only line of input contains non-empty string s obtained by Vasya. The string consists of digits only. The string length does not exceed 30 characters.
Output
Print the only number — the maximum amount of points Vasya could get. If Vasya is wrong and the string could not be obtained according to the rules then output number -1.
Sample Input
1234
37
9000
90
0009
-1
Hint
In the first example the string must be split into numbers 1, 2 and 34.
In the second example the string must be split into numbers 90, 0 and 0.
In the third example the string is incorrect, because after splitting the string into 3 numbers number 00 or 09 will be obtained, but numbers cannot have leading zeroes.
题意:将一个字符串分成三部分,然后求三部分加起来最大值,每部分不能有前导零,也不能大于1e6.
解法:直接枚举中间的两个端点判断即可。
代码如下:
#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<utility>
#include<stack>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
#include<set>
#include<map>
using namespace std;
typedef long long ll;
const int INF = 1e6;
char str[35];
ll a, b, c, result, ans = -1;
ll myget(int s, int e) {
ll tmp = 0;
for(int i = s; i <= e; i++) {
tmp = tmp * 10 + str[i] - '0';
}
return tmp;
}
int main() {
#ifndef ONLINE_JUDGE
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
#endif
scanf("%s", str);
int len = strlen(str);
for(int i = 0; i < len; i++) {
for(int j = i + 1; j < len - 1; j++) {
if((i + 1) > 7 || (j - (i + 1) + 1) > 7 || (len - 1 - (j + 1) + 1) > 7)
continue;
int l1 = i - 0 + 1, l2 = j - (i + 1) + 1, l3 = len - 1 - (j + 1) + 1;
if((str[0] == '0' && l1 != 1) || (str[i + 1] == '0' && l2 != 1)
|| (str[j + 1] == '0' && l3 != 1))
continue;
a = myget(0, i);
b = myget(i + 1, j);
c = myget(j + 1, len - 1);
if(a > INF || b > INF || c > INF)
continue;
result = a + b + c;
if(result > ans)
ans = result;
}
}
printf("%I64d\n", ans);
return 0;
}