I'm trying to implement bignum division by powers of 10. Specifically, this is a kattis problem: https://open.kattis.com/problems/divideby100
I believe my answer is correct but I get a time limit exceeded warning. I know it's due to the insert function I keep using. My question is, what, if any, are faster alternatives to insert? Or am I crazy and just going about this problem all wrong by treating the input as strings?
#include <cstdlib>
#include <iostream>
#include <vector>
#include <stdio.h>
#include <string>
#include <algorithm>
using namespace std;
int main(){
string N;
string M;
getline(cin, N);
getline(cin, M);
long long MSize = M.size();
long long NSize = N.size();
long long zeroesInM = MSize - 1;
if(NSize >= MSize){
N.insert(NSize - zeroesInM, ".");
NSize++;
}else{
long long zeroesInN = N.size() - 1;
for(long long i = 0; i < zeroesInM - zeroesInN - 1; i++){
N.insert(0, "0");
NSize++;
}
N.insert(0, ".");
N.insert(0, "0");
NSize += 2;
}
long long numTrailingZeros = NSize;
while (N[numTrailingZeros - 1] == '0'){
numTrailingZeros--;
}
numTrailingZeros = NSize - numTrailingZeros;
N.erase(NSize-numTrailingZeros, numTrailingZeros);
NSize -= numTrailingZeros + 1;
if(N[NSize] == '.'){
N.erase(NSize);
}
cout << N << endl;
return 0;
}