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

#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include <vector>
using namespace std;

int main() {
  while (true) {
    string t;
    string line;
    while (getline(cin, line) && line.size()) {
      t += line;
    }
    getline(cin, line);
    if (!line.size())
      break;
    string s = line;
    getline(cin, line);

    map<char, int> need;
    for (string ::iterator it = s.begin(); it != s.end(); it++) {
      need[*it]++;
    }

    map<char, int> cnt;
    int left = 0, right = 0;
    int occ, OCC;

    occ = 0;
    OCC = need.size();

    vector<string> v;

    while (left < t.size()) {
      while (right < t.size() && occ < OCC) {
        char c = t[right++];
        if (need[c] && need[c] == ++cnt[c])
          ++occ;
      }
      if (occ != OCC)
        break;
      while (left < right) {
        char c = t[left];
        if (need[c] && need[c] == cnt[c])
          break;
        --cnt[c];
        left++;
      }
      if (left == right)
        break;
      v.push_back(t.substr(left, right - left));
      --cnt[t[left++]];
      --occ;
    }

    int mlen = 1 << 24;
    for (int i = 0; i < v.size(); i++) {
      mlen = min(mlen, (int)v[i].size());
    }

    int answer1 = 0;
    string answer2 = "";
    for (int i = 0; i < v.size(); i++) {
      if (v[i].size() == mlen) {
        if (answer2.size() == 0) {
          answer2 = v[i];
        }
        answer1++;
      }
    }

    cout << answer1 << endl;
    if (answer1 > 0)
      cout << endl;
    for (int i = 0; i < answer2.size(); i++) {
      cout << answer2[i];
      if ((i + 1) % 72 == 0)
        cout << endl;
    }
    if (answer2.size() > 0 && answer2.size() != 72)
      cout << endl;
    cout << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

// AOJ 1215
// URL: http://judge.u-aizu.ac.jp/onlinejudge/description.jsp?id=1215

#include <algorithm>
#include <cstdio>
#include <iostream>
#include <numeric>
#include <sstream>
#include <string>
#include <vector>
using namespace std;

const int size = 51;
int cnt[size]; // cnt[i]: KÌiÔÚªgíêÄ¢éñ
int ksize;

// ðiª¶ñÉKÉÜÜêé¶ªSÄgíêÄ¢éjð½µÄ¢é©
bool check() {
  for (int i = 0; i < ksize; i++) {
    if (cnt[i] == 0)
      return false;
  }
  return true;
}

// ¶cªKÉÜÜêé
int contain(string &K, char c) {
  for (int i = 0; i < ksize; i++) {
    if (c == K[i])
      return i;
  }
  return -1;
}

void solve(string &S, string &K) {
  // ú»ÈÇ
  int li = 0, ri = 0;
  int min_cnt =
      0; // ¶ªÅ¬Ìª¶ñÌB0Èçßéª¶ñÍ¶ÝµÈ¢
  int min_length;
  int min_li, min_ri;
  bool left = false;
  for (int i = 0; i < K.size(); i++)
    cnt[i] = 0;
  ksize = K.size();
  int t; // ¿åÁÆµ½àÌÉg¤

  // Úæè
  while (li < S.size() && ri < S.size() && li <= ri) {
    for (int i = 0; i < ksize; i++)
      cnt[i] = 0;
    for (int i = li; i <= ri; i++) {
      if ((t = contain(K, S[i])) != -1)
        cnt[t]++;
    }

    // ðð½·ÜÅE¤ðEÖ¸ç·
    while (!check() && ri < S.size()) {
      ri++;
      if ((t = contain(K, S[ri])) != -1)
        cnt[t]++;
    }
    if (ri >= S.size())
      break;
    int length = ri - li + 1;
    if (length < min_length) {
      min_cnt = 1;
      min_length = length;
      min_li = li;
      min_ri = ri;
    } else if (length == min_length) {
      min_cnt++;
    }

    // ¶¤ðEÖ¸ç·
    li++;
    while (li > ri)
      ri++;
  }

  // oÍ
  string answer1, answer2;
  ostringstream os;
  os << min_cnt;
  answer1 = os.str();
  if (min_cnt > 0)
    answer2 = S.substr(min_li, min_ri - min_li + 1);

  cout << answer1 << endl;
  if (min_cnt > 0) {
    int len = answer2.size();
    for (int i = 0; i < len; i++) {
      if (i % 72 == 0)
        putchar('\\n');
      putchar(answer2[i]);
    }
    putchar('\\n');
  }
  putchar('\\n');
}

int main() {
  // üÍ
  string line;
  while (getline(cin, line) && line != "") {
    vector<string> S;
    while (line != "") {
      S.push_back(line);
      getline(cin, line);
    }
    getline(cin, line);
    if (line == "")
      break;
    string K = line;
    getline(cin, line);

    string S_con = accumulate(S.begin(), S.end(), string(""));
    solve(S_con, K);
  }
  return 0;
}