Problem p03865 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <set>
#include <string>
#include <vector>

typedef long long ll;

using namespace std;

#define all(x) x.begin(), x.end()

const ll INF = 1e9;

string s;
ll cnt[160];
ll ps[160];

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> s;
  fill(ps, ps + 160, INF);
  for (int i = 0; i < s.size(); i++)
    cnt[s[i]]++;
  for (int i = 0; i < s.size(); i++) {
    if (ps[s[i]] == INF)
      ps[s[i]] = i;
  }
  if (s[0] == s[s.size() - 1]) {
    cout << 100 / 0;
  }
  for (char c = s[0]; c <= s[0]; c++) {
    for (char d = s[s.size() - 1]; d <= s[s.size() - 1]; d++) {
      if (c == d)
        continue;
      if (cnt[c] == 0 || cnt[d] == 0)
        continue;
      if (ps[c] > ps[d])
        continue;
      int x = 1;
      int cnt = 0;
      for (int i = 0; i < s.size(); i++) {
        if (s[i] == c && x == 1) {
          x = 2;
          cnt++;
        }
        if (s[i] == d && x == 2) {
          x = 1;
          cnt++;
        }
      }
      if ((s.size() - cnt) % 2 == 0) {
        // cout << c << " " << d << endl;
        cout << "Second";
        return 0;
      }
    }
  }
  cout << "First";
  return 0;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <set>
#include <string>
#include <vector>

typedef long long ll;

using namespace std;

#define all(x) x.begin(), x.end()

const ll INF = 1e9;

string s;
ll cnt[160];
ll ps[160];

signed main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> s;
  fill(ps, ps + 160, INF);
  for (int i = 0; i < s.size(); i++)
    cnt[s[i]]++;
  for (int i = 0; i < s.size(); i++) {
    if (ps[s[i]] == INF)
      ps[s[i]] = i;
  }
  if (s[0] == s[s.size() - 1]) {
    for (char c = s[0]; c <= s[0]; c++) {
      for (char d = 'a'; d <= 'z'; d++) {
        if (c == d)
          continue;
        if (cnt[c] == 0 || cnt[d] == 0)
          continue;
        if (ps[c] > ps[d])
          continue;
        int x = 1;
        int cnt = 0;
        for (int i = 0; i < s.size(); i++) {
          if (s[i] == c && x == 1) {
            x = 2;
            cnt++;
          }
          if (s[i] == d && x == 2) {
            x = 1;
            cnt++;
          }
        }
        if ((s.size() - cnt) % 2 == 0) {
          // cout << c << " " << d << endl;
          cout << "Second";
          return 0;
        }
      }
    }
    cout << "First";
    return 0;
  }
  for (char c = s[0]; c <= s[0]; c++) {
    for (char d = s[s.size() - 1]; d <= s[s.size() - 1]; d++) {
      if (c == d)
        continue;
      if (cnt[c] == 0 || cnt[d] == 0)
        continue;
      if (ps[c] > ps[d])
        continue;
      int x = 1;
      int cnt = 0;
      for (int i = 0; i < s.size(); i++) {
        if (s[i] == c && x == 1) {
          x = 2;
          cnt++;
        }
        if (s[i] == d && x == 2) {
          x = 1;
          cnt++;
        }
      }
      if ((s.size() - cnt) % 2 == 0) {
        // cout << c << " " << d << endl;
        cout << "Second";
        return 0;
      }
    }
  }
  cout << "First";
  return 0;
}