Problem p01306 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;

int Tanni(string);

int main() {
  int i, j, n;
  string st, str, str2;
  cin >> n;
  for (int t = 0; t < n; t++) {
    int keta, a[1003], aa, dot;

    cin >> st >> str;

    dot = -1;
    bool f = false;
    for (i = 0; i < st.size(); i++) {
      if (st[i] == '.') {
        a[i] = -1;
        dot = i;
        f = true;
      } else
        a[i] = st[i] - '0';
    }
    aa = i;

    if (dot == 0) {
      for (i = aa - 1; i >= 0; i--)
        a[i + 1] = a[i];
      a[0] = 0;
      dot = 1;
      aa++;
    } else if (dot == aa - 1)
      a[aa++] = 0;

    if (!f) {
      a[aa] = -1;
      dot = aa;
      a[aa + 1] = 0;
      aa += 2;
    }

    keta = Tanni(str);
    if (keta == 0)
      str2 = str;
    else
      cin >> str2;

    while (a[0] == 0 || dot != 1) {
      if (a[0] != 0 && a[1] == -1)
        break;

      // for(dot=0;dot<aa;dot++) if(a[dot] == -1) break;

      if (a[0] == 0) {
        keta--;
        int c;
        c = a[dot];
        a[dot] = a[dot + 1];
        a[dot + 1] = c;
        for (j = 0; j < aa - 1; j++)
          a[j] = a[j + 1];
        a[aa - 1] = 0;
        aa--;
      }

      else if (dot != 1) {
        keta++;
        int c;
        c = a[dot];
        a[dot] = a[dot - 1];
        a[dot - 1] = c;
        dot--;
      }
    }

    if (!f)
      aa--;
    if (aa - 1 == dot)
      aa--;

    for (i = 0; i < aa; i++) {
      if (i == dot)
        cout << ".";
      else
        cout << a[i];
    }

    cout << " * 10^" << keta << " " << str2 << endl;
  }
  return 0;
}

int Tanni(string str) {
  if (str == "yotta")
    return 24;
  else if (str == "zetta")
    return 21;
  else if (str == "exa")
    return 18;
  else if (str == "peta")
    return 15;
  else if (str == "tera")
    return 12;
  else if (str == "giga")
    return 9;
  else if (str == "mega")
    return 6;
  else if (str == "kilo")
    return 3;
  else if (str == "hecto")
    return 2;
  else if (str == "deca")
    return 1;
  else if (str == "deci")
    return -1;
  else if (str == "centi")
    return -2;
  else if (str == "milli")
    return -3;
  else if (str == "micro")
    return -6;
  else if (str == "nano")
    return -9;
  else if (str == "pico")
    return -12;
  else if (str == "femto")
    return -15;
  else if (str == "ato")
    return -18;
  else if (str == "zepto")
    return -21;
  else if (str == "yocto")
    return -24;
  else
    return 0;
};

Error: TLE

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <iostream>
#include <string>
using namespace std;

int Tanni(string);

int main() {
  int i, j, n;
  string st, str, str2;
  cin >> n;
  for (int t = 0; t < n; t++) {
    int keta, a[1003], aa, dot;

    cin >> st >> str;

    dot = -1;
    bool f = false;
    for (i = 0; i < st.size(); i++) {
      if (st[i] == '.') {
        a[i] = -1;
        dot = i;
        f = true;
      } else
        a[i] = st[i] - '0';
    }
    aa = i;

    if (dot == 0) {
      for (i = aa - 1; i >= 0; i--)
        a[i + 1] = a[i];
      a[0] = 0;
      dot = 1;
      aa++;
    } else if (dot == aa - 1)
      a[aa++] = 0;

    if (!f) {
      a[aa] = -1;
      dot = aa;
      a[aa + 1] = 0;
      aa += 2;
    }

    keta = Tanni(str);
    if (keta == 0)
      str2 = str;
    else
      cin >> str2;

    while (a[0] == 0 || dot != 1) {
      if (a[0] != 0 && a[1] == -1)
        break;

      // for(dot=0;dot<aa;dot++) if(a[dot] == -1) break;

      if (a[0] == 0) {
        keta--;
        int c;
        c = a[dot];
        a[dot] = a[dot + 1];
        a[dot + 1] = c;
        for (j = 0; j < aa - 1; j++)
          a[j] = a[j + 1];
        a[aa - 1] = 0;
        aa--;
      }

      else if (a[1] != 1) {
        keta++;
        int c;
        c = a[dot];
        a[dot] = a[dot - 1];
        a[dot - 1] = c;
        dot--;
      }
    }

    if (!f)
      aa--;
    if (aa - 1 == dot)
      aa--;

    for (i = 0; i < aa; i++) {
      if (i == dot)
        cout << ".";
      else
        cout << a[i];
    }

    cout << " * 10^" << keta << " " << str2 << endl;
  }
  return 0;
}

int Tanni(string str) {
  if (str == "yotta")
    return 24;
  else if (str == "zetta")
    return 21;
  else if (str == "exa")
    return 18;
  else if (str == "peta")
    return 15;
  else if (str == "tera")
    return 12;
  else if (str == "giga")
    return 9;
  else if (str == "mega")
    return 6;
  else if (str == "kilo")
    return 3;
  else if (str == "hecto")
    return 2;
  else if (str == "deca")
    return 1;
  else if (str == "deci")
    return -1;
  else if (str == "centi")
    return -2;
  else if (str == "milli")
    return -3;
  else if (str == "micro")
    return -6;
  else if (str == "nano")
    return -9;
  else if (str == "pico")
    return -12;
  else if (str == "femto")
    return -15;
  else if (str == "ato")
    return -18;
  else if (str == "zepto")
    return -21;
  else if (str == "yocto")
    return -24;
  else
    return 0;
};