#include <iostream>
#include <string>
using namespace std;
string multiply(string num, char c)
{
if(num[0] == '0' || c == '0') return "0";
string result(num.length(), '0');
int carry = 0;
int mul = c - '0';
for (int i = num.length()-1; i >=0 ; --i)
{
int tmp = (num[i] - '0') * mul + carry;
result[i] = tmp % 10 + '0';
carry = tmp / 10;
}
if (carry > 0)result.insert(result.begin(), char('0' + carry));
return result;
}
string multiply(string num1, string num2)
{
if(num1[0] == '0' || num2[0] == '0') return "0";
string result(num1.length()+num2.length(), '0');
string cache[10];
for (int i = num2.length()-1; i>=0; --i)
{
int index = num2[i] - '0';
if (cache[index].size() <= 0)
cache[index] = multiply(num1, num2[i]);
string& add = cache[index];
int k = result.length() - (num2.length()-i);
int j = add.length()-1;
int carry = 0;
while (j >= 0)
{
int tmp = (result[k] - '0') + (add[j] - '0') + carry;
result[k] = tmp%10 + '0';
carry = tmp / 10;
--j, --k;
}
while (carry > 0)
{
int tmp = (result[k] - '0') + carry;
result[k] = tmp%10 + '0';
carry = tmp / 10;
--k;
}
}
if (result[0]=='0') result.erase(result.begin());
return result;
}
int main(int argc, char** argv)
{
cout << multiply("99", "988");
return 0;
}