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

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {

  string str[2];
  string under;
  pair<int, int> center;
  center.first = 500;
  center.second = 500;
  char field2[1000][1000];

  while (cin >> str[0] && str[0] != "-") {
    bool pl[200];
    memset(pl, 0, sizeof(pl));

    cin >> str[1] >> under;
    for (int i = 0; i < under.size(); i++) {
      pl[under[i]] = true;
    }
    vector<int> v;
    for (int i = 0; i < str[1].size() - 1; i++) {
      v.push_back(1);
    }
    // under
    for (int i = 0; i < str[0].size(); i++) {
      v.push_back(0);
    }
    sort(v.begin(), v.end());
    //	do{
    // Rs[µÄ­é
    memset(field2, 0, sizeof(field2));
    for (int i = 0; i < str[0].size(); i++) {
      field2[center.first - 1 - i][center.second] = str[0][i];
    }
    for (int i = 0; i < str[1].size(); i++) {
      field2[center.first][center.second - 1 - i + 1] = str[1][i];
    }

    // ±±ÅV~[V
    for (int i = 0; i < v.size(); i++) {
      if (pl[field2[500][500]]) {
        // ºÖ¨­é
        for (int i = 999; i >= 1; i--) {
          field2[i][500] = field2[i - 1][500];
        }
      } else {
        // EÖ¨­é
        for (int i = 999; i >= 1; i--) {
          field2[500][i] = field2[500][i - 1];
        }
      }
    }
    // EÖ¨­é
    for (int i = 999; i >= 1; i--) {
      field2[500][i] = field2[500][i - 1];
    }

    string ret1, ret2;
    for (int i = 501; i < 1000; i++) {
      if ((field2[500][i] >= 'a' && field2[500][i] <= 'z') ||
          (field2[500][i] >= 'A' && field2[500][i] <= 'Z')) {
        ret1 += field2[500][i];
      } else
        break;
    }
    for (int i = 501; i < 1000; i++) {
      if ((field2[i][500] >= 'a' && field2[i][500] <= 'z') ||
          (field2[i][500] >= 'A' && field2[i][500] <= 'Z')) {
        ret2 += field2[i][500];
      } else
        break;
    }
    reverse(ret2.begin(), ret2.end());
    if (ret2 == under) {
      reverse(ret1.begin(), ret1.end());
      cout << ret1 << endl;
      //	break;
    }
    //}while(next_permutation(v.begin(),v.end()));
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main() {

  string str[2];
  string under;
  pair<int, int> center;
  center.first = 500;
  center.second = 500;
  //	char field[1000][1000];
  char field2[1000][1000];

  while (cin >> str[0] && str[0] != "-") {
    cin >> str[1] >> under;
    // memset(field,0,sizeof(field));
    // for(int i = 0; i < str[0].size(); i++){
    //	field[center.first-1-i][center.second]=str[0][i];
    // }
    // for(int i = 0; i < str[1].size(); i++){
    //	field[center.first][center.second-1-i+1]=str[1][i];
    // }
    vector<int> v;
    for (int i = 0; i < str[1].size() - 1; i++) {
      v.push_back(1);
    }
    // under
    for (int i = 0; i < str[0].size(); i++) {
      v.push_back(0);
    }
    sort(v.begin(), v.end());
    do {
      // Rs[µÄ­é
      memset(field2, 0, sizeof(field2));
      for (int i = 0; i < str[0].size(); i++) {
        field2[center.first - 1 - i][center.second] = str[0][i];
      }
      for (int i = 0; i < str[1].size(); i++) {
        field2[center.first][center.second - 1 - i + 1] = str[1][i];
      }

      // for(int i = 0; i < 1000; i++)
      //	for(int j = 0; j < 1000; j++)
      //		field2[i][j]=field[i][j];
      //  ±±ÅV~[V
      for (int i = 0; i < v.size(); i++) {
        if (v[i] == 0) {
          // ºÖ¨­é
          for (int i = 999; i >= 1; i--) {
            field2[i][500] = field2[i - 1][500];
          }
        } else {
          // EÖ¨­é
          for (int i = 999; i >= 1; i--) {
            field2[500][i] = field2[500][i - 1];
          }
        }
      }
      // EÖ¨­é
      for (int i = 999; i >= 1; i--) {
        field2[500][i] = field2[500][i - 1];
      }

      string ret1, ret2;
      for (int i = 501; i < 1000; i++) {
        if ((field2[500][i] >= 'a' && field2[500][i] <= 'z') ||
            (field2[500][i] >= 'A' && field2[500][i] <= 'Z')) {
          ret1 += field2[500][i];
        } else
          break;
      }
      for (int i = 501; i < 1000; i++) {
        if ((field2[i][500] >= 'a' && field2[i][500] <= 'z') ||
            (field2[i][500] >= 'A' && field2[i][500] <= 'Z')) {
          ret2 += field2[i][500];
        } else
          break;
      }
      reverse(ret2.begin(), ret2.end());
      if (ret2 == under) {
        reverse(ret1.begin(), ret1.end());
        cout << ret1 << endl;
        break;
      }
    } while (next_permutation(v.begin(), v.end()));
  }

  return 0;
}