Problem p01175 (C++) — buggy submission, verdict: Memory Limit Exceeded

// Problem E : Optimal Rest

#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main() {
  // x\\»ðÅãÉRðÁ¦½êÌ«ÅSÊèðÀ×½àÌ
  string str[] = {"R1......", "R1.....", "R1....", "R1...", "R1..",    "R1.",
                  "R16..",    "R16.",    "R16",    "R1",    "R2.....", "R2....",
                  "R2...",    "R2..",    "R2.",    "R2",    "R32.",    "R32",
                  "R4....",   "R4...",   "R4..",   "R4.",   "R4",      "R64",
                  "R8...",    "R8..",    "R8.",    "R8"};
  // ãÌÅx\\»Ì·³ð«ºµ½àÌ
  int back[] = {127, 126, 124, 120, 112, 96, 7,  6,  4,  64, 63, 62, 60, 56,
                48,  32,  3,   2,   31,  30, 28, 24, 16, 1,  15, 14, 12, 8};
  static int len[3200001];
  len[0] = 0;
  for (int i = 1; i <= 3200000; i++) {
    len[i] = len[i - 1] + 3;
    for (int j = 0; j < 28; j++) {
      if (i - back[j] < 0)
        continue;
      len[i] = min(len[i], len[i - back[j]] + (int)str[j].size());
    }
  }
  int test;
  cin >> test;
  string s;
  while (test--) {
    cin >> s;
    // üÍ¶ñª\\»·éxÌ·³ð²×é
    for (int i = 0; i < s.size(); i++)
      if (s[i] == 'R')
        s[i] = ' ';
    istringstream iss(s);
    string tmp;
    int lest = 0;
    while (iss >> tmp) {
      tmp = "R" + tmp;
      for (int i = 0; i < 28; i++)
        if (tmp == str[i])
          lest += back[i];
    }
    while (lest > 0) {
      for (int i = 0; i < 28; i++) {
        if (lest - back[i] < 0)
          continue;
        if (len[lest] == len[lest - back[i]] + str[i].size()) {
          cout << str[i];
          lest -= back[i];
          break;
        }
      }
    }
    cout << endl;
  }
}

Error: MLE

Fixed submission:

// Problem E : Optimal Rest

#include <iostream>
#include <sstream>
#include <string>

using namespace std;

int main() {
  int test;
  cin >> test;
  string s;
  // x\\»ðÅãÉRðÁ¦½êÌ«ÅSÊèðÀ×½àÌ
  string str[] = {"R1......", "R1.....", "R1....", "R1...", "R1..",    "R1.",
                  "R16..",    "R16.",    "R16",    "R1",    "R2.....", "R2....",
                  "R2...",    "R2..",    "R2.",    "R2",    "R32.",    "R32",
                  "R4....",   "R4...",   "R4..",   "R4.",   "R4",      "R64",
                  "R8...",    "R8..",    "R8.",    "R8"};
  // ãÌÅx\\»Ì·³ð«ºµ½àÌ
  int back[] = {127, 126, 124, 120, 112, 96, 7,  6,  4,  64, 63, 62, 60, 56,
                48,  32,  3,   2,   31,  30, 28, 24, 16, 1,  15, 14, 12, 8};
  // dp[length][type] :
  // lengthÌxð\\»·é½ßÉtypeÌxð½Âg¤©
  static unsigned short dp[3200001][28];
  // length[i] : ·³iÌx\\»Ég¤¶ñ·
  static int length[3200001];
  for (int i = 0; i < 28; i++)
    dp[0][i] = 0;
  length[0] = 0;
  // OÅ¦ðSÊèßÄ¨­
  for (int i = 1; i <= 3200001; i++) {
    // ·³i-1ÉR64ðvXµ½ê
    for (int j = 0; j < 28; j++)
      dp[i][j] = dp[i - 1][j] + (j == 23);
    // bèÌÅZ\\»Ì·³
    int curLen = 0;
    for (int j = 0; j < 28; j++)
      curLen += dp[i][j] * str[j].size();
    for (int j = 0; j < 28; j++) {
      // jÔÚÌxÌ·³ªßÁ½É·³ª}CiXÈçcontinue
      if (i - back[j] < 0)
        continue;
      // ·³i-back[j]Ì\\»ÉjÔÚÌxð«µ½Ì·³ð²×é
      int newLen = length[i - back[j]] + str[j].size();
      bool flag = false;
      // æèZ¢ê
      if (newLen < curLen)
        flag = true;
      // ¯¶·³Èç«är
      else if (newLen == curLen) {
        for (int k = 0; k < 28; k++) {
          // «æªÌx\\»ð½­g¤ûª«ÅOÌ\\»ÉÈé
          if (dp[i][k] < dp[i - back[j]][k])
            flag = true;
          if (dp[i][k] != dp[i - back[j]][k])
            break;
        }
      }
      // \\ÌXV
      if (flag) {
        for (int k = 0; k < 28; k++) {
          dp[i][k] = dp[i - back[j]][k] + (k == j);
        }
        curLen = newLen;
      }
    }
    length[i] = curLen;
  }
  while (test--) {
    cin >> s;
    // üÍ¶ñª\\»·éxÌ·³ð²×é
    for (int i = 0; i < s.size(); i++)
      if (s[i] == 'R')
        s[i] = ' ';
    istringstream iss(s);
    string tmp;
    int len = 0;
    while (iss >> tmp) {
      tmp = "R" + tmp;
      for (int i = 0; i < 28; i++)
        if (tmp == str[i])
          len += back[i];
    }
    // Î·é¶ñðoÍ·é
    for (int i = 0; i < 28; i++) {
      for (int j = 0; j < dp[len][i]; j++)
        cout << str[i];
    }
    cout << endl;
  }
}