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

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

#define MOD 10000000
using namespace std;
typedef long long LL;

long long quickpow(long long n, long long base) {
  long long res = 1;
  while (n) {
    if (n & 1) {
      res = res * base % MOD;
    }
    n >>= 1;
    base = base * base % MOD;
  }
  return res;
} // ¿ìËÙÃÝ
char A[100050];
char B[100050];
int main() {
  cin >> A + 1;
  while (A[1] != '.') {
    cin >> B + 1;
    int lenA = strlen(A + 1);
    int lenB = strlen(B + 1);
    int i = 1, j = 1;
    int flag = 0;
    int k = 0;
    while (i <= lenA && j <= lenB) {
      if (A[i] == B[j]) {
        if (A[i] == '"' && k) {
          k = 0;
        } else if (A[i] == '"' && !k) {
          k = 1;
        }

        i++;
        j++;
      } else if (k == 1) {
        while (A[i] != '"' && i <= lenA)
          i++;
        while (B[j] != '"' && j <= lenB)
          j++;
        flag++;
        i++;
        j++;
        k = 0;

      } else if (k == 0) {

        flag += 2;
        break;
      }
    }
    if (k == 0 && i > lenA && j <= lenB)
      flag += 2;
    if (k == 0 && j > lenB && i <= lenA)
      flag += 2;
    if (j > lenB && A[i] == '"')
      flag += 2;
    else if (i > lenA && B[j] == '"')
      flag += 2;
    else if (i > lenA && j <= lenB)
      flag++;
    else if (j > lenB && i <= lenA)
      flag++;
    if (flag == 0)
      cout << "IDENTICAL" << endl;
    if (flag == 1)
      cout << "CLOSE" << endl;
    if (flag > 1)
      cout << "DIFFERENT" << endl;
    cin >> A + 1;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
char stra[1000], strb[1000];
int lena, lenb;
stack<char> s1, s2;

int main() {
  freopen("a.in", "r", stdin);
  while (true) {
    scanf("%s", stra);
    lena = strlen(stra);
    if (lena == 1 && stra[0] == '.')
      break;
    while (!s1.empty())
      s1.pop();
    while (!s2.empty())
      s2.pop();
    scanf("%s", strb);
    lenb = strlen(strb);
    int posa = 0, posb = 0;
    int cnt = 0;
    bool ff = false;
    while (true) {
      if (posa >= lena && posb >= lenb) {
        break;
      }
      if (stra[posa] != strb[posb]) {
        printf("DIFFERENT\\n");
        ff = true;
        break;
      }
      if (stra[posa] == '\\"') {
        bool fg = false;
        posa++, posb++;
        while (true) {
          if (stra[posa] == strb[posb] && strb[posb] == '\\"') {
            //						posa ++, posb ++;
            break;
          }
          if (stra[posa] == strb[posb]) {
            posa++, posb++;
            continue;
          } else {
            cnt++;
            for (posa; posa < lena && stra[posa] != '\\"'; posa++)
              ;
            for (posb; posb < lenb && strb[posb] != '\\"'; posb++)
              ;
            fg = true;
            break;
          }
        }
        if (fg && cnt == 2) {
          printf("DIFFERENT\\n");
          ff = true;
          break;
        }
      }
      posa++, posb++;
    }
    if (cnt == 1 && !ff)
      printf("CLOSE\\n");
    else if (!ff)
      printf("IDENTICAL\\n");
  }
  return 0;
}