using
System;
using
System.Collections.Generic;
public
class
GFG {
private
static
bool
IsValid(
string
num)
{
if
(num.Length > 1 && num[0] ==
'0'
) {
return
false
;
}
return
true
;
}
private
static
int
Val(
string
a,
int
pos)
{
if
(pos >= a.Length || pos < 0) {
return
0;
}
return
(
int
)(a[pos] -
'0'
);
}
private
static
string
AddString(
string
a,
string
b)
{
char
[] sum =
new
char
[(Math.Max(a.Length, b.Length))];
int
i = a.Length - 1;
int
j = b.Length - 1;
int
carry = 0;
while
(i >= 0 || j >= 0) {
int
t = Val(a, i) + Val(b, j) + carry;
sum[(Math.Max(i, j))] = (
char
)(t % 10 +
'0'
);
carry = t / 10;
i--;
j--;
}
if
(carry > 0) {
return
carry.ToString()
+
new
string
(sum).TrimEnd(
'\0'
);
}
return
new
string
(sum).TrimEnd(
'\0'
);
}
private
static
bool
CheckAddition(List<
string
> res,
string
a,
string
b,
string
c)
{
if
(!IsValid(a) || !IsValid(b)) {
return
false
;
}
string
sum = AddString(a, b);
if
(sum.Equals(c)) {
res.Add(sum);
return
true
;
}
if
(c.Length <= sum.Length
|| !sum.Equals(c.Substring(0, sum.Length))) {
return
false
;
}
else
{
res.Add(sum);
return
CheckAddition(res, b, sum,
c.Substring(sum.Length));
}
}
public
static
List<
string
> AdditiveSequence(
string
num)
{
List<
string
> res =
new
List<
string
>();
int
l = num.Length;
for
(
int
i = 1; i <= l / 2; i++) {
for
(
int
j = 1; j <= (l - i) / 2; j++) {
if
(CheckAddition(res, num.Substring(0, i),
num.Substring(i, j),
num.Substring(i + j))) {
res.Insert(0, num.Substring(0, i));
res.Insert(1, num.Substring(i, j));
return
res;
}
}
}
res.Clear();
return
res;
}
public
static
void
Main(
string
[] args)
{
string
num =
"235813"
;
List<
string
> res = AdditiveSequence(num);
if
(res.Count > 0)
Console.WriteLine(
"True "
);
else
Console.WriteLine(
"False "
);
num =
"199100199"
;
res = AdditiveSequence(num);
if
(res.Count > 0)
Console.WriteLine(
"True "
);
else
Console.WriteLine(
"False "
);
}
}