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


//		HYSO SERIOUS? WHY SO SERIOUS                            RIOUS? WHY
//SO SERIOUS? WHYSO SERIO 		 ERIOUS? WHY SO SERIOUS ? WHY WHY SO SERIOUS? WHY
//SOSERIOUSWHYS 		       OSERIOUS? WHY SO SERIOU         SERIOUS        ERIOUS? WHY SO
//SERIOUS? WHYS 		          WHY SO SERIOUS? WHY SOSERIOUS? WHYSOSERIOUS? WHY SO SERIOUS?WHY
//SO SE 		           ERIOUS? WHY SO SERIOUS? WHY SO SERIOUS? WHY SO SERIOUS? WHY SOSERIO 		           WHY
//SO SERIOUS? WHY SO SERIOUS? WHY SO SERIOUS? WHY SO SERIOUS? WH 		            SERIOUS?WHYSO
//SERIOUS? WHY SO SERIOUS? WHY SO SERIOUS? WHYSO SE 		              ?WHYSOSERIOUS?WHY SO
//SERIOUS? WHY SO SERIOUS?WHYSO SERIOUS? 		                           OSERIOUS? WHY SO SERIOUS?WHYSOSER
//		                                 HYSOSERIOUS?WHYSOSERI
//		                                     SOSERIOUS?W
//		                                         SER
//		                                          W

#include <iostream>
#include <queue>
#include <tuple>
#include <vector>
using namespace std;
#include <algorithm>
#include <bits/stdc++.h>
#define ll long long int
#define read(a)                                                                \\
  long long int a;                                                             \\
  cin >> a;
#define readstr(s)                                                             \\
  string s;                                                                    \\
  cin >> s;
#define readarr(a, l)                                                          \\
  long long int a[l] = {0};                                                    \\
  for (ll i = 0; i < l; i++)                                                   \\
    cin >> a[i];
#define loop(i, a, b) for (ll i = a; i < b; i++)
#define rloop(i, a, b) for (ll i = a; i > b; i--)
#define ff first
#define ss second
#define readmtrx(i, j, a, b)                                                   \\
  ll arr[a][b] = {0};                                                          \\
  for (ll i = 0; i < a; i++)                                                   \\
    for (ll j = 0; j < b; j++)                                                 \\
      cin >> arr[i][j];
#define v(pi) vector<ll> pi
#define vp(pi) vector<pair<ll, ll>> pi
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);
#define pb(p) push_back(p)
#define mp make_pair
#include <cmath>
#include <map>
#include <string>
#define mi(lol) map<ll, ll> lol
#define fsort(vec) sort(vec.begin(), vec.end())
#define rsort(vec) sort(vec.begin(), vec.end(), compare)
#define inf ll inf = INT_MAX
// sorting according to the second element vector

bool compare(pair<ll, ll> p, pair<ll, ll> q) { return p.second < q.second; }

//   example:-
//   sort(vec.begin(),vec.end(),compare);

int main() {
  fast;
  // #ifndef ONLINE_JUDGE
  //   freopen("input.txt","r",stdin);
  //   freopen("output.txt","w",stdout);
  // #endif
  readstr(s) string f = s;
  bool flag = false, flag1 = false;
  ll m = s.length() / 2;
  string g = s.substr(0, m);
  string rr = g;
  reverse(g.begin(), g.end());
  if (rr == g) {
    flag = true;
  }
  string r = s.substr(m + 1, s.length() - 1);
  string rrr = r;
  reverse(r.begin(), r.end());
  if (rrr == r) {
    flag1 = true;
  }
  bool flag2 = false;
  reverse(s.begin(), s.end());
  if (s == f)
    flag2 = true;
  if (flag1 && flag && flag2)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}


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


Fixed submission:


//		HYSO SERIOUS? WHY SO SERIOUS                            RIOUS? WHY
//SO SERIOUS? WHYSO SERIO 		 ERIOUS? WHY SO SERIOUS ? WHY WHY SO SERIOUS? WHY
//SOSERIOUSWHYS 		       OSERIOUS? WHY SO SERIOU         SERIOUS        ERIOUS? WHY SO
//SERIOUS? WHYS 		          WHY SO SERIOUS? WHY SOSERIOUS? WHYSOSERIOUS? WHY SO SERIOUS?WHY
//SO SE 		           ERIOUS? WHY SO SERIOUS? WHY SO SERIOUS? WHY SO SERIOUS? WHY SOSERIO 		           WHY
//SO SERIOUS? WHY SO SERIOUS? WHY SO SERIOUS? WHY SO SERIOUS? WH 		            SERIOUS?WHYSO
//SERIOUS? WHY SO SERIOUS? WHY SO SERIOUS? WHYSO SE 		              ?WHYSOSERIOUS?WHY SO
//SERIOUS? WHY SO SERIOUS?WHYSO SERIOUS? 		                           OSERIOUS? WHY SO SERIOUS?WHYSOSER
//		                                 HYSOSERIOUS?WHYSOSERI
//		                                     SOSERIOUS?W
//		                                         SER
//		                                          W

#include <iostream>
#include <queue>
#include <tuple>
#include <vector>
using namespace std;
#include <algorithm>
#include <bits/stdc++.h>
#define ll long long int
#define read(a)                                                                \\
  long long int a;                                                             \\
  cin >> a;
#define readstr(s)                                                             \\
  string s;                                                                    \\
  cin >> s;
#define readarr(a, l)                                                          \\
  long long int a[l] = {0};                                                    \\
  for (ll i = 0; i < l; i++)                                                   \\
    cin >> a[i];
#define loop(i, a, b) for (ll i = a; i < b; i++)
#define rloop(i, a, b) for (ll i = a; i > b; i--)
#define ff first
#define ss second
#define readmtrx(i, j, a, b)                                                   \\
  ll arr[a][b] = {0};                                                          \\
  for (ll i = 0; i < a; i++)                                                   \\
    for (ll j = 0; j < b; j++)                                                 \\
      cin >> arr[i][j];
#define v(pi) vector<ll> pi
#define vp(pi) vector<pair<ll, ll>> pi
#define fast                                                                   \\
  ios_base::sync_with_stdio(false);                                            \\
  cin.tie(0);
#define pb(p) push_back(p)
#define mp make_pair
#include <cmath>
#include <map>
#include <string>
#define mi(lol) map<ll, ll> lol
#define fsort(vec) sort(vec.begin(), vec.end())
#define rsort(vec) sort(vec.begin(), vec.end(), compare)
#define inf ll inf = INT_MAX
// sorting according to the second element vector

bool compare(pair<ll, ll> p, pair<ll, ll> q) { return p.second < q.second; }

//   example:-
//   sort(vec.begin(),vec.end(),compare);

int main() {
  fast;
#ifndef ONLINE_JUDGE
  freopen("input.txt", "r", stdin);
  freopen("output.txt", "w", stdout);
#endif
  readstr(s) bool flag = false, flag1 = false;
  ll m = s.length() / 2;
  string g = s.substr(0, m);
  string rr = g;
  reverse(g.begin(), g.end());
  if (rr == g) {
    flag = true;
  }
  string r = s.substr(m + 1, s.length() - 1);
  string rrr = r;
  reverse(r.begin(), r.end());
  if (rrr == r) {
    flag1 = true;
  }
  if (flag1 && flag)
    cout << "Yes" << endl;
  else
    cout << "No" << endl;
  return 0;
}