Title Link: https://leetcode.com/problems/additive-number/
Title Description: A string of numbers is additive number, there must be a division to divide the number of numbers into n numbers, n must be greater than 3, and in addition to the first two digits, each number is the sum of the first two numbers, in addition, there is no number containing leading zeros.
Topic Analysis: Deep Search
Code implementation:
#include <map>#include<vector>#include<algorithm>#include<cstring>#include<cstdlib>#include<cstdio>#include<cmath>#include<iostream>#include<stack>using namespacestd;BOOLJudgestringAstringBstringsum) { //cout<<a<< "," <<b<< "," <<sum<<endl; intLena =a.length (); intLenB =b.length (); intLENS =sum.length (); if(Lens < Lena | | Lens < LENB)return 0; intc =0; while(Lena >0&& LenB >0) { intTMP = a[lena-1] + b[lenb-1] -'0'-'0'+C; C= tmp/Ten; TMP= tmp%Ten; if(sum[lens-1]! = tmp+'0')return 0; Lena--; LenB--; Lens--; } while(Lena >0) { intTMP = a[lena-1] + C-'0'; C= tmp/Ten; TMP= tmp%Ten; if(sum[lens-1]! = tmp+'0')return 0; Lena--; Lens--; } while(LenB >0) { intTMP = b[lenb-1] + C-'0'; C= tmp/Ten; TMP= tmp%Ten; if(sum[lens-1]! = tmp+'0')return 0; LenB--; Lens--; } if((c==0&& lens==0) || (lens==1&& sum[0]==c+'0')) return 1; return 0;}BOOLdfstringAstringBstringnum) { intLena =a.length (); intLenB =b.length (); intLenn =num.length (); intLen =Max (Lena, LenB); if(!lenn)return 1; if(Len > Lenn)return 0; if((a[0] =='0'&& lena>1) || (b[0] =='0'&& lenb>1) || (num[0] =='0'&& lenn>1))return 0; BOOLRET = Judge (A, B, Num.substr (0, Len)); //cout<<a<< "," <<b<< "," <<num.substr (0, Len) << ", ret:" <<ret<< Endl; if(ret) {if(DF (b, Num.substr (0, Len), Num.substr (len ))return 1; } if(len+1> Lenn)return 0; RET= Judge (A, B, Num.substr (0, len+1)); //cout<<a<< "," <<b<< "," <<num.substr (0, len+1) << ", ret:" <<ret<< Endl; if(ret) {if(DF (b, Num.substr (0, len+1), Num.substr (len+1))) return 1; } return 0;}BOOLIsadditivenumber (stringnum) { intLen =num.length (); for(inti =0; I < len-2; i++) { for(intj = i+1; J < len-1; J + +) { if(DF (NUM.SUBSTR (0, i+1), Num.substr (i+1, j-i), Num.substr (j+1))) return 1; } } return 0;}intMain () {strings; while(cin>>s) {cout<<s<<" is"<<isadditivenumber (s) <<Endl; } return 0;}
Additive Number 306