-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathmain.cpp
53 lines (45 loc) · 1.38 KB
/
main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Author : Qi Zhang
// Date : 2018-12-11
#include <bits/stdc++.h>
using namespace std;
string multiply(string num1, string num2) {
string res(num1.size() + num2.size(), '0');
for (int i = num1.size() - 1; i >= 0; i--) {
for (int j = num2.size() - 1; j >= 0; j--) {
int prod = (num1[i] - '0') * (num2[j] - '0') + (res[i + j + 1] - '0');
res[i+j+1] = (prod % 10) + '0';
res[i+j] = ((prod /10) + (res[i + j] - '0')) + '0';
}
}
int it = res.find_first_not_of("0");
return ( it < 0 ? "0" : res.substr(it) );
}
bool check(string &s, int p, string a, string b){
if(p == s.size()) return true;
string c = multiply(a, b);
int l = c.size();
if(p+l <= s.size() && s.substr(p, l) == c) return check(s, p+l, b, c);
else return false;
}
int main()
{
string s;
while (getline(cin, s)) {
int n = s.size();
bool ans = false;
for(int i = 0; i < n; i++){
if(ans) break;
for(int j = i+1; j < n; j++){
string a = s.substr(0, i+1), b = s.substr(i+1, j-i);
string mul = multiply(a, b);
int l = mul.size();
if(j+l < n && s.substr(j+1, l) == mul && check(s, j+1, a, b)) {
ans = true;
break;
}
}
}
cout << (ans? "true": "false") << endl;
}
return 0;
}