Problem p03582 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int A, B, C;

string getans(string S) {
  int ca = 0, cb = 0, cc = 0;
  for (int i = 0; i < S.size(); i++) {
    if (S[i] == 'a')
      ca++;
    if (S[i] == 'b')
      cb++;
    if (S[i] == 'c')
      cc++;
  }
  if (cb == 0)
    return S;

  vector<string> vec;
  for (int i = 0; i < ca; i++)
    vec.push_back("a");
  for (int i = 0; i < cc; i++)
    vec[i % ca] += "c";
  sort(vec.begin(), vec.end());
  int Z = 1;
  for (int i = 1; i < vec.size(); i++) {
    if (vec[i] == vec[i - 1])
      Z = i + 1;
    else
      break;
  }
  for (int i = 0; i < cb; i++)
    vec[i % Z] += "b";
  sort(vec.begin(), vec.end());

  string G = "a";
  int I = 0;
  for (int i = 1; i < vec.size(); i++) {
    if (vec[i] != vec[i - 1])
      I++;
    G += ('a' + I);
  }
  vector<string> A = vec;
  A.erase(unique(A.begin(), A.end()), A.end());
  string V = getans(G), U = "";
  for (int i = 0; i < V.size(); i++) {
    U += A[V[i] - 'a'];
  }
  return U;
}

string F;

int main() {
  cin >> A >> B >> C;
  if (A >= 1)
    F += "a";
  if (B >= 1)
    F += "b";
  if (C >= 1)
    F += "c";
  int P = 0;
  while (A == 0) {
    A = B;
    B = C;
    C = 0;
  }
  while (B == 0) {
    B = C;
    C = 0;
  }

  string G = "";
  for (int i = 0; i < A; i++)
    G += "a";
  for (int i = 0; i < B; i++)
    G += "b";
  for (int i = 0; i < C; i++)
    G += "c";

  string ret = getans(G);
  for (int i = 0; i < ret.size(); i++)
    ret[i] = F[ret[i] - 'a'];
  cout << ret << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int A, B, C;

string getans(string S) {
  int ca = 0, cb = 0, cc = 0;
  for (int i = 0; i < S.size(); i++) {
    if (S[i] == 'a')
      ca++;
    if (S[i] == 'b')
      cb++;
    if (S[i] == 'c')
      cc++;
  }
  if (cb == 0)
    return S;

  vector<string> vec;
  for (int i = 0; i < ca; i++)
    vec.push_back("a");
  for (int i = 0; i < cc; i++)
    vec[i % ca] += "c";
  sort(vec.begin(), vec.end());
  int Z = 1;
  for (int i = 1; i < vec.size(); i++) {
    if (vec[i] == vec[i - 1])
      Z = i + 1;
    else
      break;
  }
  for (int i = 0; i < cb; i++)
    vec[i % Z] += "b";
  sort(vec.begin(), vec.end());

  string G = "a";
  int I = 0;
  for (int i = 1; i < vec.size(); i++) {
    if (vec[i] != vec[i - 1])
      I++;
    G += ('a' + I);
  }
  vector<string> A = vec;
  A.erase(unique(A.begin(), A.end()), A.end());
  string V = getans(G), U = "";
  for (int i = 0; i < V.size(); i++) {
    U += A[V[i] - 'a'];
  }
  return U;
}

string F;

int main() {
  cin >> A >> B >> C;
  if (A >= 1)
    F += "a";
  if (B >= 1)
    F += "b";
  if (C >= 1)
    F += "c";
  int P = 0;
  while (A == 0) {
    A = B;
    B = C;
    C = 0;
  }
  while (B == 0 && C >= 1) {
    B = C;
    C = 0;
  }

  string G = "";
  for (int i = 0; i < A; i++)
    G += "a";
  for (int i = 0; i < B; i++)
    G += "b";
  for (int i = 0; i < C; i++)
    G += "c";

  string ret = getans(G);
  for (int i = 0; i < ret.size(); i++)
    ret[i] = F[ret[i] - 'a'];
  cout << ret << endl;
  return 0;
}