Problem p03863 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
using ll = long long;
using ull = unsigned long long;

#define FOR(i, m, n) for (int i = int(m); i < int(n); i++)
#define REFOR(i, m, n) for (int i = int(n - 1); i >= int(m); i--)
#define REP(i, n) for (int i = 0; i < int(n); i++)
#define REREP(i, n) for (int i = int(n - 1); i >= 0; i--)
#define VI vector<int>
#define VVI vector<vector<int>>
#define VVVI vector<vector<vector<int>>>
#define VL vector<ll>
#define VVL vector<vector<ll>>
#define VB vector<bool>
#define VVB vector<vector<bool>>
#define PAIR pair<int, int>
#define MP make_pair
#define VP vector<pair<int, int>>
#define VS vector<string>
#define MAP map<int, int>
#define QUE queue<int>
#define DEQ deque<int>
#define PQUE priority_queue<int> // 5,5,4,3,3,2,...
#define REPQUE                                                                 \
  priority_queue<int, vector<int>, greater<int>> // 1,1,2,3,4,4,5,...
#define SUM(obj) accumulate((obj).begin(), (obj).end(), 0)
#define SORT(obj) sort((obj).begin(), (obj).end()) // 1,2,3,4,5...
#define RESORT(obj)                                                            \
  sort((obj).begin(), (obj).end(), greater<int>())            // 5,4,3,2,1...
#define UB(obj, n) upper_bound((obj).begin(), (obj).end(), n) // itr > n
#define LB(obj, n) lower_bound((obj).begin(), (obj).end(), n) // itr>= n

const ll MOD = (ll)1e9 + 7;
const ll INF = (ll)1e17;

int gcd(int a, int b) {
  if (a == 0 || b == 0)
    return 0;
  if (a < b)
    swap(a, b);
  while (b != 0) {
    a = a % b;
    swap(a, b);
  }
  return a;
}

int lcm(int a, int b) {
  if (a == 0 || b == 0)
    return 0;
  return a / gcd(a, b) * b;
}

vector<bool> Eratosthenes(int N) {
  vector<bool> Eratosthenes(N + 1, true);
  Eratosthenes[0] = false;
  if (N == 0)
    return Eratosthenes;
  Eratosthenes[1] = false;
  for (int i = 1; i * i <= N; i++)
    if (Eratosthenes[i])
      for (int j = 2 * i; j <= N; j += i)
        Eratosthenes[j] = false;
  return Eratosthenes;
}

bool pcheck(int N) {
  if (N == 0 || N == 1)
    return false;
  for (int i = 2; i * i <= N; i++)
    if (N % i == 0)
      return false;
  return true;
}

////combination
///mod-------------------------------------------------------------------------
//
// vector<ll> f = factorial(n, quotient);//erase this line when you use!
//
////vector including modulus of N!
// vector<ll> factorial(ll N, ll quotient) {
//	vector<ll> factorial(N + 1, 1);
//	for (ll i = 1; i <= N; i++) {
//		factorial[i] *=factorial[i - 1]*i;
//		factorial[i] %= quotient;
//	}
//	return factorial;
// }
//
////repeat square method y = x^n mod quotient
// ll rsm(ll x,ll n,ll quotient){
//	ll y = 1;
//	while (n > 0) {
//		if ((n & 1) == 1) {
//			y *= x;
//			y %= quotient;
//		}
//		x *= x;
//		x %= quotient;
//		n >>= 1;
//	}
//	return y;
// }
//
////modulus of nCk - combination mod
// ll nCk(ll n, ll k, ll quotient) {
//
//	ll nCk = (f[n] % quotient);
//	nCk *= rsm(f[k], quotient - 2, quotient);
//	nCk %= quotient;
//	nCk *= rsm(f[n - k], quotient - 2, quotient);
//	return (nCk % quotient);
// }
//------------------------------------------------------------

////union-find---------------------------------------------------
// int node[10];
//
// int root(int n) {
//	if (node[n] == n) return n;
//	else return node[n] = root(node[n]);
// }
//
// void unite(int n, int m) {
//	if (n > m) swap(n, m);
//	n = root(n);
//	m = root(m);
//	if (n == m) return;
//	else node[m] = n;
// }
//-------------------------------------------------------------

// void dijkstra(int start) {
//	priority_queue<PAIR, vector<PAIR>, greater<PAIR>> pq;
//	dist[start][start] = 0;
//	pq.push({ 0,start });
//	while (!pq.empty()) {
//		int from = pq.top().second;
//		pq.pop();
//		REP(i, edge[from].size()) {
//			int to = edge[from][i].first;
//			if (dist[start][to] > dist[start][from] +
//edge[from][i].second) { 				dist[start][to] = dist[start][from] +
//edge[from][i].second; 				pq.push({ dist[start][from] + edge[from][i].second,to
//});
//			}
//		}
//	}
// }

void ANS(bool flag) { cout << ((flag) ? "YES" : "NO") << endl; }

void Ans(bool flag) { cout << ((flag) ? "Yes" : "No") << endl; }

void ans(bool flag) { cout << ((flag) ? "yes" : "no") << endl; }

int main() {
  string S;
  cin >> S;
  int N = S.size();
  list<int> ls;
  REP(i, N) ls.push_back(int(S[i] - 'a'));
  // cout << N << "  ";
  // for (auto itr = ls.begin(); itr != ls.end(); itr++) {
  //	cout << *itr << " ";
  // }
  // cout << endl;

  int i = 0;
  bool flag = true;
  while (1) {
    flag = false;
    N = ls.size();

    if (N > 2) {
      auto itrR = ls.begin();
      auto itrL = itrR;
      itrR++;
      auto itrC = itrR;
      itrR++;
      for (;; itrL++, itrC++, itrR++) {
        // cout << *itrL << " " << *itrC << " " << *itrR << endl;
        if (itrR == ls.end())
          break;

        if (*itrL != *itrR) {
          ls.erase(itrC);
          flag = true;
          break;
        }
      }
    }

    // N = ls.size();
    // cout << N << "  ";
    // for(auto itr = ls.begin(); itr != ls.end(); itr++){
    //	cout << *itr << " ";
    // }
    // cout << endl;

    i++;
    if (!flag)
      break;
  }
  cout << ((i % 2 == 0) ? "First" : "Second") << endl;
  // cin >> S;
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <deque>
#include <fstream>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <vector>

using namespace std;
using ll = long long;
using ull = unsigned long long;

#define FOR(i, m, n) for (int i = int(m); i < int(n); i++)
#define REFOR(i, m, n) for (int i = int(n - 1); i >= int(m); i--)
#define REP(i, n) for (int i = 0; i < int(n); i++)
#define REREP(i, n) for (int i = int(n - 1); i >= 0; i--)
#define VI vector<int>
#define VVI vector<vector<int>>
#define VVVI vector<vector<vector<int>>>
#define VL vector<ll>
#define VVL vector<vector<ll>>
#define VB vector<bool>
#define VVB vector<vector<bool>>
#define PAIR pair<int, int>
#define MP make_pair
#define VP vector<pair<int, int>>
#define VS vector<string>
#define MAP map<int, int>
#define QUE queue<int>
#define DEQ deque<int>
#define PQUE priority_queue<int> // 5,5,4,3,3,2,...
#define REPQUE                                                                 \
  priority_queue<int, vector<int>, greater<int>> // 1,1,2,3,4,4,5,...
#define SUM(obj) accumulate((obj).begin(), (obj).end(), 0)
#define SORT(obj) sort((obj).begin(), (obj).end()) // 1,2,3,4,5...
#define RESORT(obj)                                                            \
  sort((obj).begin(), (obj).end(), greater<int>())            // 5,4,3,2,1...
#define UB(obj, n) upper_bound((obj).begin(), (obj).end(), n) // itr > n
#define LB(obj, n) lower_bound((obj).begin(), (obj).end(), n) // itr>= n

const ll MOD = (ll)1e9 + 7;
const ll INF = (ll)1e17;

int gcd(int a, int b) {
  if (a == 0 || b == 0)
    return 0;
  if (a < b)
    swap(a, b);
  while (b != 0) {
    a = a % b;
    swap(a, b);
  }
  return a;
}

int lcm(int a, int b) {
  if (a == 0 || b == 0)
    return 0;
  return a / gcd(a, b) * b;
}

vector<bool> Eratosthenes(int N) {
  vector<bool> Eratosthenes(N + 1, true);
  Eratosthenes[0] = false;
  if (N == 0)
    return Eratosthenes;
  Eratosthenes[1] = false;
  for (int i = 1; i * i <= N; i++)
    if (Eratosthenes[i])
      for (int j = 2 * i; j <= N; j += i)
        Eratosthenes[j] = false;
  return Eratosthenes;
}

bool pcheck(int N) {
  if (N == 0 || N == 1)
    return false;
  for (int i = 2; i * i <= N; i++)
    if (N % i == 0)
      return false;
  return true;
}

////combination
///mod-------------------------------------------------------------------------
//
// vector<ll> f = factorial(n, quotient);//erase this line when you use!
//
////vector including modulus of N!
// vector<ll> factorial(ll N, ll quotient) {
//	vector<ll> factorial(N + 1, 1);
//	for (ll i = 1; i <= N; i++) {
//		factorial[i] *=factorial[i - 1]*i;
//		factorial[i] %= quotient;
//	}
//	return factorial;
// }
//
////repeat square method y = x^n mod quotient
// ll rsm(ll x,ll n,ll quotient){
//	ll y = 1;
//	while (n > 0) {
//		if ((n & 1) == 1) {
//			y *= x;
//			y %= quotient;
//		}
//		x *= x;
//		x %= quotient;
//		n >>= 1;
//	}
//	return y;
// }
//
////modulus of nCk - combination mod
// ll nCk(ll n, ll k, ll quotient) {
//
//	ll nCk = (f[n] % quotient);
//	nCk *= rsm(f[k], quotient - 2, quotient);
//	nCk %= quotient;
//	nCk *= rsm(f[n - k], quotient - 2, quotient);
//	return (nCk % quotient);
// }
//------------------------------------------------------------

////union-find---------------------------------------------------
// int node[10];
//
// int root(int n) {
//	if (node[n] == n) return n;
//	else return node[n] = root(node[n]);
// }
//
// void unite(int n, int m) {
//	if (n > m) swap(n, m);
//	n = root(n);
//	m = root(m);
//	if (n == m) return;
//	else node[m] = n;
// }
//-------------------------------------------------------------

// void dijkstra(int start) {
//	priority_queue<PAIR, vector<PAIR>, greater<PAIR>> pq;
//	dist[start][start] = 0;
//	pq.push({ 0,start });
//	while (!pq.empty()) {
//		int from = pq.top().second;
//		pq.pop();
//		REP(i, edge[from].size()) {
//			int to = edge[from][i].first;
//			if (dist[start][to] > dist[start][from] +
//edge[from][i].second) { 				dist[start][to] = dist[start][from] +
//edge[from][i].second; 				pq.push({ dist[start][from] + edge[from][i].second,to
//});
//			}
//		}
//	}
// }

void ANS(bool flag) { cout << ((flag) ? "YES" : "NO") << endl; }

void Ans(bool flag) { cout << ((flag) ? "Yes" : "No") << endl; }

void ans(bool flag) { cout << ((flag) ? "yes" : "no") << endl; }

int main() {
  string S;
  cin >> S;
  int N = S.size();
  if (S[0] == S[N - 1] && N % 2 == 0)
    cout << "First" << endl;
  if (S[0] == S[N - 1] && N % 2 == 1)
    cout << "Second" << endl;
  if (S[0] != S[N - 1] && N % 2 == 0)
    cout << "Second" << endl;
  if (S[0] != S[N - 1] && N % 2 == 1)
    cout << "First" << endl;
  return 0;
}