Problem p02571 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define pb(a) push_back(a)
#define ms(x, y) memset(x, y, sizeof(x))
#define sci(a) scanf("%d", &a)
#define scl(a) scanf("%lld", &a)
#define scd(a) scanf("%lf", &a)
#define ll unsigned long long
using namespace std;
/* --------------- fast io --------------- */ // begin
namespace Fread {
const int SIZE = 1 << 20;
char buf[SIZE], *S, *T;
inline char getchar() {
  if (S == T) {
    T = (S = buf) + fread(buf, 1, SIZE, stdin);
    if (S == T)
      return EOF;
  }
  return *S++;
}
} // namespace Fread
namespace Fwrite {
const int SIZE = 1 << 20;
char buf[SIZE], *S = buf, *T = buf + SIZE;
inline void flush() {
  fwrite(buf, 1, S - buf, stdout);
  S = buf;
}
inline void putchar(char c) {
  *S++ = c;
  if (S == T)
    flush();
}
struct _ {
  ~_() { flush(); }
} __;
} // namespace Fwrite

#ifdef ONLINE_JUDGE
#define getchar Fread::getchar
#define putchar Fwrite::putchar
#endif

template <typename T> inline void read(T &x) {
  x = 0;
  int f = 1;
  char c = getchar();
  while (!isdigit(c)) {
    if (c == '-')
      f = -1;
    c = getchar();
  }
  while (isdigit(c))
    x = x * 10 + (c - '0'), c = getchar();
  x *= f;
}
template <typename T> inline void write(T x, bool _enter = 0, bool _space = 0) {
  if (!x)
    putchar('0');
  else {
    if (x < 0)
      putchar('-'), x = -x;
    static char dig[41];
    int top = 0;
    while (x)
      dig[++top] = (x % 10) + '0', x /= 10;
    while (top)
      putchar(dig[top--]);
  }
  if (_enter)
    putchar('\\n');
  if (_space)
    putchar(' ');
}

namespace Fastio {
struct reader {
  template <typename T> reader &operator>>(T &x) {
    ::read(x);
    return *this;
  }
  reader &operator>>(char &c) {
    c = getchar();
    while (c == '\\n' || c == ' ')
      c = getchar();
    return *this;
  }
  reader &operator>>(char *str) {
    int len = 0;
    char c = getchar();
    while (c == '\\n' || c == ' ')
      c = getchar();
    while (c != '\\n' && c != ' ')
      str[len++] = c, c = getchar();
    str[len] = '\\0';
    return *this;
  }
} cin;
const char endl = '\\n';
struct writer {
  template <typename T> writer &operator<<(T x) {
    ::write(x, 0, 0);
    return *this;
  }
  writer &operator<<(char c) {
    putchar(c);
    return *this;
  }
  writer &operator<<(const char *str) {
    int cur = 0;
    while (str[cur])
      putchar(str[cur++]);
    return *this;
  }
} cout;
} // namespace Fastio
// #define cin Fastio::cin
#define cout Fastio::cout
#define endl Fastio::endl
/* --------------- fast io --------------- */ // end

const int maxn = 100005;
ll a[maxn];
ll an[maxn];
const ll m = 1e9 + 7;
int main() {
  string s1, s2;
  cin >> s1 >> s2;
  int n1 = s1.length(), n2 = s2.length();
  int ans = n2;
  for (int i = 0; i <= n1 - n2; ++i) {
    int t = 0;
    for (int j = 0; j < n2; ++j)
      if (s1[i + j] != s2[j])
        t++;
    ans = min(ans, t);
  }

  cout << ans << endl;
  return 0;
}

Error: WA

Fixed submission:

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>

using namespace std;
string s, t;
int a[100] = {0};
int sum = 0;
int dfs(int x, int y) {
  for (int i = 1; i + y < t.size(); i++) {
    if (s[x + i] == t[y + i])
      sum++;
  }
}
int main() {
  cin >> s >> t;
  int len1, len2;
  len1 = s.size();
  len2 = t.size();
  int ans = len2;
  for (int j = 0; j < len2; j++)
    a[t[j] - 'a'] = 1;
  for (int i = 0; i < len1; i++) {

    if (a[s[i] - 'a'] == 1) {
      if (len2 > i) {
        for (int j = 0; j <= i; j++) {
          if (len1 - i - 1 < len2 - j - 1)
            break;
          if (s[i] == t[j]) {
            sum++;
            dfs(i, j);
            ans = min(ans, len2 - sum);
            sum = 0;
          }
        }
      } else {
        for (int j = 0; j < len2; j++) {
          if (len1 - i - 1 < len2 - j - 1)
            j = len2 - (len1 - i);
          if (s[i] == t[j]) {
            sum++;
            dfs(i, j);
            ans = min(ans, len2 - sum);
            sum = 0;
          }
        }
      }
    }
  }
  cout << ans;
}