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

/*" بِسْمِ اللهِ الرَّحْمٰنِ الرَّحِيْمِ -In the name of Allah."
   Never stop chasing!-MD.SOURAV.

   ░░░░░░░░( •̪●)░░░░░░░░░░░░░░░░░░░░░░░░
   ░░░░░░███████ ]▄▄▄▄▄▄▄▄▃░░░▃░░░░ ▃░
   ▂▄▅█████████▅▄▃▂░░░░░░░░░░░░░░░░░
   [███████████████████].░░░░░░░░░░░░░░
   ◥⊙▲⊙▲⊙▲⊙▲⊙▲⊙▲⊙◤...░░░░░░░░░░░░
*/

#include <bits/stdc++.h>

#define endl "\\n"
#define MAX 32000
#define F first
#define S second
#define sz(x) x.size()
#define li long int
#define gcd(a, b) _gcd(a, b)
#define pb push_back
#define asrt(a, n) sort(a, a + n)
#define MOD 1000000007
#define ll long long int
#define lcd(a, b) (a * b) / gcd(a, b)
#define mp(x, y) make_pair(x, y)
#define no cout << "NO" << endl
#define yes cout << "YES" << endl
#define f(i, a, n) for (int i = a; i < n; i++)
#define test                                                                   \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define srt(v) sort(v.begin(), v.end())
#define basrt(a, n) sort(a, a + n, greater<int>())
#define deb(x) cout << #x << ": " << x << endl;
#define sumof(x) accumulate(x.begin(), x.end(), 0);
#define rsrt(v) sort(v.begin(), v.end(), greater<int>())
#define enter(v, n)                                                            \\
  for (ll i = 0; i < n; i++) {                                                 \\
    ll x;                                                                      \\
    cin >> x;                                                                  \\
    v.pb(x);                                                                   \\
  }
#define unique(v) v.erase(unique(v.begin(), v.end()), v.end())
#define start                                                                  \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define ignore cin.ignore(std::numeric_limits<std::streamsize>::max(), '\\n');

using namespace std;

double pi =
    3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664;
typedef vector<ll> vll;
typedef vector<int> vii;
typedef map<ll, ll> mllll;
typedef map<char, ll> mchll;
typedef map<string, ll> msll;
typedef vector<pair<ll, ll>> vpllll;
typedef vector<pair<int, int>> vpiiii;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << '{';
  for (const auto &x : v)
    os << " " << x;
  return os << '}';
}

void solve() {

  string s;
  cin >> s;
  string a;
  cin >> a;
  int mn = a.size();
  if (s == a) {
    cout << "0";
    return;
  }
  int size_of_a = a.size();
  int size_of_s = s.size();
  for (int i = 0; i < (size_of_s - size_of_a) + 1; i++) {
    string x = s.substr(i, size_of_a);
    // deb(x);
    int cnt = 0;
    for (int j = 0; j < size_of_a; j++) {
      if (x[j] != a[j]) {
        cnt++;
      }
    }
    mn = min(mn, cnt);
  }
  cout << mn << endl;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll test_case = 1;
  // cin>>test_case;
  while (test_case--) {
    solve();
  }

  return 0;
}

Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  basic_string::substr: __pos (which is 7) > this->size() (which is 6)


Fixed submission:

/*" بِسْمِ اللهِ الرَّحْمٰنِ الرَّحِيْمِ -In the name of Allah."
   Never stop chasing!-MD.SOURAV.

   ░░░░░░░░( •̪●)░░░░░░░░░░░░░░░░░░░░░░░░
   ░░░░░░███████ ]▄▄▄▄▄▄▄▄▃░░░▃░░░░ ▃░
   ▂▄▅█████████▅▄▃▂░░░░░░░░░░░░░░░░░
   [███████████████████].░░░░░░░░░░░░░░
   ◥⊙▲⊙▲⊙▲⊙▲⊙▲⊙▲⊙◤...░░░░░░░░░░░░
*/

#include <bits/stdc++.h>

#define endl "\\n"
#define MAX 32000
#define F first
#define S second
#define sz(x) x.size()
#define li long int
#define gcd(a, b) _gcd(a, b)
#define pb push_back
#define asrt(a, n) sort(a, a + n)
#define MOD 1000000007
#define ll long long int
#define lcd(a, b) (a * b) / gcd(a, b)
#define mp(x, y) make_pair(x, y)
#define no cout << "NO" << endl
#define yes cout << "YES" << endl
#define f(i, a, n) for (int i = a; i < n; i++)
#define test                                                                   \\
  int t;                                                                       \\
  cin >> t;                                                                    \\
  while (t--)
#define srt(v) sort(v.begin(), v.end())
#define basrt(a, n) sort(a, a + n, greater<int>())
#define deb(x) cout << #x << ": " << x << endl;
#define sumof(x) accumulate(x.begin(), x.end(), 0);
#define rsrt(v) sort(v.begin(), v.end(), greater<int>())
#define enter(v, n)                                                            \\
  for (ll i = 0; i < n; i++) {                                                 \\
    ll x;                                                                      \\
    cin >> x;                                                                  \\
    v.pb(x);                                                                   \\
  }
#define unique(v) v.erase(unique(v.begin(), v.end()), v.end())
#define start                                                                  \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define ignore cin.ignore(std::numeric_limits<std::streamsize>::max(), '\\n');

using namespace std;

double pi =
    3.14159265358979323846264338327950288419716939937510582097494459230781640628620899862803482534211706798214808651328230664;
typedef vector<ll> vll;
typedef vector<int> vii;
typedef map<ll, ll> mllll;
typedef map<char, ll> mchll;
typedef map<string, ll> msll;
typedef vector<pair<ll, ll>> vpllll;
typedef vector<pair<int, int>> vpiiii;

template <typename T> ostream &operator<<(ostream &os, const vector<T> &v) {
  os << '{';
  for (const auto &x : v)
    os << " " << x;
  return os << '}';
}

void solve() {

  string s;
  cin >> s;
  string a;
  cin >> a;
  int mn = 10000;
  int size_of_a = a.size();
  for (int i = 0; i < (s.size() - size_of_a) + i; i++) {
    string x = s.substr(i, i + size_of_a);
    // deb(x);
    int cnt = 0;
    for (int j = 0; j < size_of_a; j++) {
      if (x[j] != a[j]) {
        cnt++;
      }
    }
    mn = min(mn, cnt);
  }
  cout << mn << endl;
}

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);
  ll test_case = 1;
  // cin>>test_case;
  while (test_case--) {
    solve();
  }

  return 0;
}