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

#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
typedef long long int lli;
using namespace std;

/*
int main(){
        int n,k;
        while(cin>>n>>k&&n!=0&&k!=0){
                vector<int> card(n+1);
                vector<int> flag(n+1);
                flag[0]=-1;
                card[0]=-1;
                for(int i=0;i<k; ++i){
                        int tmp;
                        cin >> tmp;
                        card[tmp]=tmp;
                        flag[tmp]=1;
                }
                int ans=0;
                if(flag[0]!=0){
                        int cnt=0;
                        int s;
                        int n;
                        for(int i=1;i<=n; ++i){
                                if()
                        }
                }
        }
        return 0;
}*/

int main() {
  string A, B;
  while (cin >> A >> B) {
    int ans = 0;
    for (int i = 0; i < A.size(); ++i) {
      string A_dash = A.substr(i);
      vector<int> flag(A_dash.size() > B.size() ? B.size() : A_dash.size());
      for (int j = 0; j < flag.size(); ++j) {
        if (A_dash[j] == B[j])
          flag[j] = 1;
      }
      int cnt = 0;
      for (int j = 0; j < flag.size(); ++j) {
        if (flag[j] == 1) {
          ++cnt;
          ans = max(ans, cnt);
        } else if (flag[j] == 0)
          cnt = 0;
      }
    }
    for (int i = 0; i < B.size(); ++i) {
      string B_dash = B.substr(i);
      vector<int> flag(B_dash.size() > A.size() ? A.size() : B_dash.size());
      for (int j = 0; j < flag.size(); ++j) {
        if (B_dash[j] == A[j])
          flag[j] = 1;
      }
      int cnt = 0;
      for (int j = 0; j < flag.size(); ++j) {
        if (flag[j] == 1) {
          ++cnt;
          ans = max(ans, cnt);
        } else if (flag[j] == 0)
          cnt = 0;
      }
    }
    cout << ans << "\\n";
  }
  return 0;
}

/*
int main(){
        string A;
        while(cin>>A){
                int ans=0;
                int cnt=0;
                int tmp;
                for(int j=0; j<A.size();++j){
                        //cout << cnt << "\\n";
                        if(A[j]=='1') {
                                ++cnt;
                                ans=max(ans,cnt);
                        }else if(A[j]=='0') cnt=0;
                                tmp=A[j];
                }
                cout << ans << "\\n";
        }
        return 0;
}*/

Error: TLE

Fixed submission:

#include <algorithm>
#include <cstdlib>
#include <fstream>
#include <iostream>
#include <iterator>
#include <map>
#include <queue>
#include <stack>
#include <string>
#include <vector>
typedef long long int lli;
using namespace std;

/*
int main(){
        int n,k;
        while(cin>>n>>k&&n!=0&&k!=0){
                vector<int> card(n+1);
                vector<int> flag(n+1);
                flag[0]=-1;
                card[0]=-1;
                for(int i=0;i<k; ++i){
                        int tmp;
                        cin >> tmp;
                        card[tmp]=tmp;
                        flag[tmp]=1;
                }
                int ans=0;
                if(flag[0]!=0){
                        int cnt=0;
                        int s;
                        int n;
                        for(int i=1;i<=n; ++i){
                                if()
                        }
                }
        }
        return 0;
}*/

int main() {
  string A, B;
  while (cin >> A >> B) {
    int ans = 0;
    for (int i = 0; i < A.size(); ++i) {
      for (int j = ans; i + j <= A.size(); ++j) {
        string tmp = A.substr(i, j);
        /*for(int k=i;k<j; ++k){
                tmp+=A[k];
        }*/
        int a = tmp.size();
        if (B.find(tmp) != -1)
          ans = max(ans, a);
      }
    }
    cout << ans << "\\n";
  }
  return 0;
}