prithivMLmods/Coder-Stat huggingface dataset:
Problem p03337 (Python)

Buggy submission (status: Runtime Error):
def main(a, b):
    add = a + b
    sub = a - b
    mul = a * b

    return max(add, sub, mul)


if __name__ == "__main__":
    a, b = input().split(" ")
    print(main(a, b))


Fixed submission:
def main(a, b):
    add = a + b
    sub = a - b
    mul = a * b

    return max(add, sub, mul)


if __name__ == "__main__":
    a, b = map(int, input().split(" "))
    print(main(a, b))
Problem p03338 (Python)

Buggy submission (status: Runtime Error):
n = int(input())
s = input()
num = [len(set(s[:i]) & set(s[i:]) for i in range(n))]
print(max(num))


Fixed submission:
n = int(input())
s = input()
num = [len(set(s[:i]) & set(s[i:])) for i in range(n)]
print(max(num))
Problem p03338 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  string s;
  cin >> n >> s;
  int i, j;
  int ans = 0;
  int ansmax = 0;
  vector<bool> kind(26, false);
  vector<bool> kind2(26, false);
  for (i = 1; i < n; i++) {
    for (j = 0; j < i; j++) {
      if (kind.at(s.at(j) - 'a') == false) {
        kind.at(s.at(j) - 'a') = true;
      }
    }
    for (j = i; j < n; j++) {
      if (kind2.at(s.at(j) - 'a') == false) {
        kind2.at(s.at(j) - 'a') = true;
      }
    }
    ans = 0;
    for (j = 0; j < n; j++) {
      if (kind.at(j) == true && kind2.at(j) == true)
        ans++;
      kind.at(j) = false;
      kind2.at(j) = false;
    }
    if (ans > ansmax)
      ansmax = ans;
  }
  cout << ansmax << endl;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int main() {
  int n;
  string s;
  cin >> n >> s;
  int i, j;
  int ans = 0;
  int ansmax = 0;
  vector<bool> kind(26, false);
  vector<bool> kind2(26, false);
  for (i = 1; i < n; i++) {
    for (j = 0; j < i; j++) {
      if (kind.at(s.at(j) - 'a') == false) {
        kind.at(s.at(j) - 'a') = true;
      }
    }
    for (j = i; j < n; j++) {
      if (kind2.at(s.at(j) - 'a') == false) {
        kind2.at(s.at(j) - 'a') = true;
      }
    }
    ans = 0;
    for (j = 0; j < 26; j++) {
      if (kind.at(j) == true && kind2.at(j) == true)
        ans++;
      kind.at(j) = false;
      kind2.at(j) = false;
    }
    if (ans > ansmax)
      ansmax = ans;
  }
  cout << ansmax << endl;
}
Problem p03338 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
int f(string s, int i);
int main() {
  int n, i, j, ans = 0;
  int temp;
  string s;
  cin >> n;
  cin >> s;
  for (int i = 1; i < n; i++) {
    temp = f(s, i);
    ans = max(ans, temp);
  }
  printf("%d", ans);
  return 0;
}
int f(string s, int i) {
  string s1 = s.substr(0, i);
  string s2 = s.substr(i);
  char x = 'a';
  int n1[26], n2[26], ans = 0;
  memset(n1, 0, sizeof(n1));
  memset(n2, 0, sizeof(n2));
  for (int j = 0; i < 26; j++) {
    n1[j] = count(s1.begin(), s1.end(), x);
    n2[j] = count(s2.begin(), s2.end(), x);
    x++;
  }
  for (int j = 0; j < 26; j++) {
    if (n1[j] > 0 && n2[j] > 0)
      ans++;
  }
  return ans;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stdlib.h>
#include <string>
#include <vector>
using namespace std;
int f(string s, int i);
int main() {
  int n, i, j, ans = 0;
  int temp;
  string s;
  cin >> n;
  cin >> s;
  for (int i = 1; i < n; i++) {
    temp = f(s, i);
    ans = max(ans, temp);
  }
  printf("%d", ans);
  return 0;
}
int f(string s, int i) {
  string s1 = s.substr(0, i);
  string s2 = s.substr(i);
  char x = 'a';
  int n1[26], n2[26], ans = 0;
  memset(n1, 0, sizeof(n1));
  memset(n2, 0, sizeof(n2));
  for (int j = 0; j < 26; j++) {
    n1[j] = count(s1.begin(), s1.end(), x);
    n2[j] = count(s2.begin(), s2.end(), x);
    x++;
  }
  for (int j = 0; j < 26; j++) {
    if (n1[j] > 0 && n2[j] > 0)
      ans++;
  }
  return ans;
}
Problem p03338 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int main() {
  int n;
  string s;
  cin >> n >> s;
  int ans = 0;
  for (int i = 1; i < n; i++) {
    int count = 0;
    for (char c = 'a'; c <= 'z'; c++) {
      bool left = false;
      bool right = false;
      for (int j = 0; j < i; j++) {
        if (s[j] == c)
          left = true;
      }
      for (int j = i; j < n; i++) {
        if (s[j] == c)
          right = true;
      }
      if (left && right)
        count++;
    }
    ans = max(ans, count);
  }
  cout << ans << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
#define rep(i, n) for (int i = 0; i < n; i++)
using namespace std;

int main() {
  int n;
  string s;
  cin >> n >> s;
  int ans = 0;
  for (int i = 1; i < n; i++) {
    int count = 0;
    for (char c = 'a'; c <= 'z'; c++) {
      bool left = false;
      bool right = false;
      for (int j = 0; j < i; j++) {
        if (s[j] == c)
          left = true;
      }
      for (int j = i; j < n; j++) {
        if (s[j] == c)
          right = true;
      }
      if (left && right)
        count++;
    }
    ans = max(ans, count);
  }
  cout << ans << endl;
  return 0;
}
Problem p03338 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
const long long INF = 1LL << 60;
#define rev(s) (string((s).rbegin(), (s).rend()))

int main() {
  int n;
  string s;
  cin >> n >> s;

  map<char, int> l, r;

  vector<int> dp(n);

  for (int i = 0; i < n; i++) {
    r[s[i]]++;
  }

  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    r[s[i]]--;
    if (r[s[i]] > 0 && l[s[i]] > 0) {
      dp[i + 1] = dp[i];
    } else if (r[s[i]] > 0 && l[s[i]] == 0) {
      dp[i + 1] = dp[i] + 1;
    } else if (r[s[i]] == 0 && l[s[i]] == 0) {
      dp[i + 1] = dp[i];
    } else if (r[s[i]] == 0 && l[s[i]] > 0) {
      dp[i + 1] = dp[i] - 1;
    }
    l[s[i]]++;
  }
  int mx = -1;
  rep(i, n) { mx = max(mx, dp[i]); }
  cout << mx << endl;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;
const long long INF = 1LL << 60;
#define rev(s) (string((s).rbegin(), (s).rend()))

int main() {
  int n;
  string s;
  cin >> n >> s;

  map<char, int> l, r;

  vector<int> dp(n + 1);

  for (int i = 0; i < n; i++) {
    r[s[i]]++;
  }

  dp[0] = 0;
  for (int i = 0; i < n; i++) {
    r[s[i]]--;
    if (r[s[i]] > 0 && l[s[i]] > 0) {
      dp[i + 1] = dp[i];
    } else if (r[s[i]] > 0 && l[s[i]] == 0) {
      dp[i + 1] = dp[i] + 1;
    } else if (r[s[i]] == 0 && l[s[i]] == 0) {
      dp[i + 1] = dp[i];
    } else if (r[s[i]] == 0 && l[s[i]] > 0) {
      dp[i + 1] = dp[i] - 1;
    }
    l[s[i]]++;
  }
  int mx = -1;
  rep(i, n) { mx = max(mx, dp[i]); }
  cout << mx << endl;
}
Problem p03338 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
  // input
  int n;
  cin >> n;
  string str;
  cin >> str;
  vector<char> alp{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
                   'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
                   's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
  vector<int> f(25, 0);
  vector<int> l(25, 0);
  vector<int> count(25, 0);
  vector<int> ex(25, 0);

  // compute
  int ans;
  ans = 0;

  for (int j = 0; j < 26; j++) {
    for (int i = 0; i < n; i++) {
      if (str.at(i) == alp[j]) {
        f[j] = i;
        ex[j] = 1;
        break;
      }
    }
    for (int i = 0; i < n - 1; i++) {
      if (str.at(n - 1 - i) == alp[j]) {
        l[j] = n - 1 - i;
        break;
      }
    }
  }

  for (int i = 0; i < 26; i++) {
    if (l[i] < f[i]) {
    } else if (ex[i] == 1) {
      for (int j = 0; j < l[i] - f[i]; j++) {
        count[f[i] + j] += 1;
      }
    }
  }

  sort(count.rbegin(), count.rend());

  ans = count[0];

  // output
  cout << ans << endl;
}

Fixed submission:
#include <algorithm>
#include <bits/stdc++.h>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
  // input
  int n;
  cin >> n;
  string str;
  cin >> str;
  vector<char> alp{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i',
                   'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
                   's', 't', 'u', 'v', 'w', 'x', 'y', 'z'};
  vector<int> f(100, 0);
  vector<int> l(100, 0);
  vector<int> count(100, 0);
  vector<int> ex(100, 0);

  // compute
  int ans;
  ans = 0;

  for (int j = 0; j < 26; j++) {
    for (int i = 0; i < n; i++) {
      if (str.at(i) == alp[j]) {
        f[j] = i;
        ex[j] = 1;
        break;
      }
    }
    for (int i = 0; i < n - 1; i++) {
      if (str.at(n - 1 - i) == alp[j]) {
        l[j] = n - 1 - i;
        break;
      }
    }
  }

  for (int i = 0; i < 26; i++) {
    if (l[i] < f[i]) {
    } else if (ex[i] == 1) {
      for (int j = 0; j < l[i] - f[i]; j++) {
        count[f[i] + j] += 1;
      }
    }
  }

  sort(count.rbegin(), count.rend());

  ans = count[0];

  // output
  cout << ans << endl;
}
Problem p03338 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<ll>;
using vii = vector<vi>;
using Pll = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(v) v.begin(), v.end()
#define sz(x) ((int)x.size())
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
const int MOD = 1000000007;
template <class T> void print(const T &t) { cout << t << endl; }
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll m = sz(s);
  ll ans = 0;
  for (ll i = 1; i < m; i++) {
    ll count = 0;
    string a = s.erase(i);
    string b = s.substr(i);
    for (char t = 'a'; t <= 'z'; t++) {
      bool judge_a = false;
      bool judge_b = false;
      for (ll j = 0; j < sz(a); j++) {
        if (a[j] == t) {
          judge_a = true;
        }
      }
      if (judge_a) {
        for (ll j = 0; j < sz(b); j++) {
          if (b[j] == t) {
            judge_b = true;
          }
        }
      }
      if (judge_a && judge_b) {
        count++;
      }
    }
    chmax(ans, count);
  }
  print(ans);
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using vi = vector<ll>;
using vii = vector<vi>;
using Pll = pair<ll, ll>;
#define rep(i, n) for (ll i = 0; i < (ll)(n); i++)
#define all(v) v.begin(), v.end()
#define sz(x) ((int)x.size())
#define pb push_back
#define mp make_pair
#define mt make_tuple
#define F first
#define S second
const int MOD = 1000000007;
template <class T> void print(const T &t) { cout << t << endl; }
template <class T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}

int main() {
  ll n;
  cin >> n;
  string s;
  cin >> s;
  ll m = sz(s);
  ll ans = 0;
  for (ll i = 1; i < m; i++) {
    ll count = 0;
    string x = s;
    string y = s;
    string a = x.erase(i);
    string b = y.substr(i);
    for (char t = 'a'; t <= 'z'; t++) {
      bool judge_a = false;
      bool judge_b = false;
      for (ll j = 0; j < sz(a); j++) {
        if (a[j] == t) {
          judge_a = true;
        }
      }
      if (judge_a) {
        for (ll j = 0; j < sz(b); j++) {
          if (b[j] == t) {
            judge_b = true;
          }
        }
      }
      if (judge_a && judge_b) {
        count++;
      }
    }
    chmax(ans, count);
  }
  print(ans);
}
Problem p03338 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;

int N;
char S[15];

int main() {
  scanf("%d", &N);
  scanf("%s", S);

  int res = 0;
  for (int i = 0; i < N - 1; i++) {
    bool asc[26] = {false};
    int cnt = 0;
    for (int k = 0; k < i + 1; k++) {
      asc[S[k] - 'a'] = true;
    }
    for (int j = i + 1; j < N; j++) {
      if (asc[S[j] - 'a']) {
        cnt++;
        asc[S[j] - 'a'] = false;
      }
    }
    res = max(res, cnt);
  }
  printf("%d", res);
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;

int N;
char S[127];

int main() {
  scanf("%d", &N);
  scanf("%s", S);

  int res = 0;
  for (int i = 0; i < N - 1; i++) {
    bool asc[26] = {false};
    int cnt = 0;
    for (int k = 0; k < i + 1; k++) {
      asc[S[k] - 'a'] = true;
    }
    for (int j = i + 1; j < N; j++) {
      if (asc[S[j] - 'a']) {
        cnt++;
        asc[S[j] - 'a'] = false;
      }
    }
    res = max(res, cnt);
  }
  printf("%d", res);
  return 0;
}
Problem p03338 (C++)

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

using namespace std;
using ll = long long;

int main() {
  ll N;
  string str;
  cin >> N;
  cin >> str;
  ll ans = 0;
  ll count = 0;
  for (size_t i = 0; i < N; i++) {
    count = 0;
    for (char c = 'a'; c <= 'z'; c++) {
      bool right = false, left = false;
      for (ll j = 0; j < i; j++) {
        if (str[j] == c)
          right = true;
      }
      for (ll j = i; j < N; j++) {
        if (str[j] == c)
          left = true;
      }
      if (right && left)
        count++;
    }
    ans = max(count, ans);
  }
  return ans;
}

Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <queue>
#include <string>

using namespace std;
using ll = long long;

int main() {
  ll N;
  string str;
  cin >> N;
  cin >> str;
  ll ans = 0;
  ll count = 0;
  for (size_t i = 0; i < N; i++) {
    count = 0;
    for (char c = 'a'; c <= 'z'; c++) {
      bool right = false, left = false;
      for (ll j = 0; j < i; j++) {
        if (str[j] == c)
          right = true;
      }
      for (ll j = i; j < N; j++) {
        if (str[j] == c)
          left = true;
      }
      if (right && left)
        count++;
    }
    ans = max(count, ans);
  }
  cout << ans << endl;
  return 0;
}
Problem p03338 (Python)

Buggy submission (status: Runtime Error):
N = int(input())
S = input()

print(max(len(set(S[:i]) & set(S[i:])) for i in range(1, N - 1)))


Fixed submission:
N = int(input())
S = input()

print(max(len(set(S[:i]) & set(S[i:])) for i in range(1, N)))
Problem p03338 (C++)

Buggy submission (status: Runtime Error):
#include <stdio.h>
#include <string.h>

char abc[] = "abcdefghijklmnopqrstuvwxyz";

int abc_pos(char str) {
  int i, ret;

  for (i = 0; i < 26; i++) {
    if (str == abc[i]) {
      ret = i;
    }
  }
  return ret;
}

int main() {
  int N, i, j, max = 0;
  char str[100];
  int flag_l[26] = {0}, flag_r[26] = {0};
  int same[26] = {0};

  scanf("%d%s", &N, str);

  for (i = 0; i < N - 1; i++) {
    for (j = 0; j <= i; j++) {
      flag_l[abc_pos(str[j])] = 1;
    }
    for (j = i + 1; j < N; j++) {
      flag_r[abc_pos(str[j])] = 1;
    }
    for (j = 0; j < 26; j++) {
      if (flag_l[j] == 1 && flag_r[j] == 1) {
        same[i]++;
      }
    }
    // printf("%d: %d\n", i, same[i]);
    if (max <= same[i]) {
      max = same[i];
    }
    for (j = 0; j < 26; j++) {
      flag_l[j] = 0;
      flag_r[j] = 0;
    }
  }
  printf("%d\n", max);

  return 0;
}


Fixed submission:
#include <stdio.h>
#include <string.h>

char abc[] = "abcdefghijklmnopqrstuvwxyz";

int abc_pos(char str) {
  int i, ret;

  for (i = 0; i < 26; i++) {
    if (str == abc[i]) {
      ret = i;
    }
  }
  return ret;
}

int main() {
  int N, i, j, max = 0;
  char str[100];
  int flag_l[26] = {0}, flag_r[26] = {0};
  int same[100] = {0};

  scanf("%d%s", &N, str);

  for (i = 0; i < N - 1; i++) {
    for (j = 0; j <= i; j++) {
      flag_l[abc_pos(str[j])] = 1;
    }
    for (j = i + 1; j < N; j++) {
      flag_r[abc_pos(str[j])] = 1;
    }
    for (j = 0; j < 26; j++) {
      if (flag_l[j] == 1 && flag_r[j] == 1) {
        same[i]++;
      }
    }
    // printf("%d: %d\n", i, same[i]);
    if (max <= same[i]) {
      max = same[i];
    }
    for (j = 0; j < 26; j++) {
      flag_l[j] = 0;
      flag_r[j] = 0;
    }
  }
  printf("%d\n", max);

  return 0;
}
Problem p03338 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <string.h>
#include <vector>

int main() {
  int N;
  std::cin >> N;
  char s[N];
  std::cin >> s;

  std::vector<char> left(26);
  std::vector<char> right(26);
  for (int i = 0; i < N; i++) {
    left[i] = 0;
    right[i] = 0;
  }

  int answer = 0;
  int answer_sub = 0;

  // for文で全体を見ていく.
  if (N == 1) {
    std::cout << 0 << std::endl;
    return 0;
  }

  // 最初に初期値について
  int iterator = 0;
  for (int i = 0; i <= N; i++) {
    iterator = s[i] - 'a';
    if (i == 0) {
      left[iterator] += 1;
    } else {
      if (left[iterator] == 1) {
        answer_sub = 1;
        answer = 1;
      }
      right[iterator] += 1;
    }
  }
  if (N == 2) {
    std::cout << answer_sub << std::endl;
    return 0;
  }
  // 右に切断箇所をずらしていく．
  int left_past = 0;
  int right_past = 0;
  for (int i = 1; i < N; i++) {
    iterator = s[i] - 'a';

    // 元々のデータ
    left_past = left[iterator];
    right_past = right[iterator];

    // 後のデータ
    left[iterator] += 1;
    right[iterator] -= 1;

    // ずらしたことによるanswerの変更について

    // 元はなかったけどずらしたことにより増えた
    if (right[iterator] > 0 && left_past == 0) {
      answer_sub++;
      if (answer <= answer_sub) {
        answer = answer_sub;
      }
    }

    // ずらしたけどセーフ
    /* if (left_past > 0 && right[iterator] > 0) {
      なにもしなくていい
    }*/

    // ずらしたら減った
    if (left_past > 0 && right[iterator] == 0) {
      answer_sub--;
    }
  }

  // answerを出力
  std::cout << answer << std::endl;
}


Fixed submission:
#include <iostream>
#include <string.h>
#include <vector>

int main() {
  int N;
  std::cin >> N;
  char s[N];
  std::cin >> s;

  std::vector<char> left(26);
  std::vector<char> right(26);
  for (int i = 0; i < 26; i++) {
    left[i] = 0;
    right[i] = 0;
  }

  int answer = 0;
  int answer_sub = 0;

  // for文で全体を見ていく.
  if (N == 1) {
    std::cout << 0 << std::endl;
    return 0;
  }

  // 最初に初期値について
  int iterator = 0;
  for (int i = 0; i <= N; i++) {
    iterator = s[i] - 'a';
    if (i == 0) {
      left[iterator] += 1;
    } else {
      if (left[iterator] == 1) {
        answer_sub = 1;
        answer = 1;
      }
      right[iterator] += 1;
    }
  }
  if (N == 2) {
    std::cout << answer_sub << std::endl;
    return 0;
  }
  // 右に切断箇所をずらしていく．
  int left_past = 0;
  int right_past = 0;
  for (int i = 1; i < N; i++) {
    iterator = s[i] - 'a';

    // 元々のデータ
    left_past = left[iterator];
    right_past = right[iterator];

    // 後のデータ
    left[iterator] += 1;
    right[iterator] -= 1;

    // ずらしたことによるanswerの変更について

    // 元はなかったけどずらしたことにより増えた
    if (right[iterator] > 0 && left_past == 0) {
      answer_sub++;
      if (answer <= answer_sub) {
        answer = answer_sub;
      }
    }

    // ずらしたけどセーフ
    /* if (left_past > 0 && right[iterator] > 0) {
      なにもしなくていい
    }*/

    // ずらしたら減った
    if (left_past > 0 && right[iterator] == 0) {
      answer_sub--;
    }
  }

  // answerを出力
  std::cout << answer << std::endl;
}
Problem p03338 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int main() {
  string alphabet = "abcdefghijklmnopqrstuvwxyz";
  int N, ans = 0;
  cin >> N;
  char S[N];
  vector<int> imos(N, 0);
  vector<int> count(26, 0);
  for (int i = 0; i < N; i++) {
    cin >> S[i];
    for (int j = 0; j < 26; j++) {
      if (S[i] == alphabet.at(j))
        count[j]++;
    }
  }
  for (int i = 0; i < N; i++) {
    if (count[i] > 1) {
      for (int j = 0; j < N; j++) {
        if (S[j] == alphabet.at(i)) {
          imos[j]++;
          break;
        }
      }
      for (int j = N - 1; j > -1; j--) {
        if (S[j] == alphabet.at(i)) {
          imos[j]--;
          break;
        }
      }
    }
  }
  int s_imos = 0;
  for (int i = 0; i < N; i++) {
    s_imos += imos[i];
    ans = max(ans, s_imos);
  }
  cout << ans;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  string alphabet = "abcdefghijklmnopqrstuvwxyz";
  int N, ans = 0;
  cin >> N;
  char S[N];
  vector<int> imos(N, 0);
  vector<int> count(26, 0);
  for (int i = 0; i < N; i++) {
    cin >> S[i];
    for (int j = 0; j < 26; j++) {
      if (S[i] == alphabet.at(j))
        count[j]++;
    }
  }
  for (int i = 0; i < 26; i++) {
    if (count[i] > 1) {
      for (int j = 0; j < N; j++) {
        if (S[j] == alphabet.at(i)) {
          imos[j]++;
          break;
        }
      }
      for (int j = N - 1; j > -1; j--) {
        if (S[j] == alphabet.at(i)) {
          imos[j]--;
          break;
        }
      }
    }
  }
  int s_imos = 0;
  for (int i = 0; i < N; i++) {
    s_imos += imos[i];
    ans = max(ans, s_imos);
  }
  cout << ans;
}
Problem p03338 (C++)

Buggy submission (status: Runtime Error):

#include <algorithm> //min, max, sort, unique, fill
#include <iostream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility> //pair, swap,
#include <vector>

#define REP(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

vector<long long> vi;

int main() {
  ll n;
  ll temp;
  string s;
  cin >> n >> s;

  for (int i = 1; i < n - 1; i++) {
    temp = 0;
    string s1 = s.substr(0, i);
    string s2 = s.substr(i, n - i);
    for (char c = 'a'; c <= 'z'; c++) {
      if (s1.find(c) != string::npos && s2.find(c) != string::npos)
        temp++;
    }
    vi.push_back(temp);
  }
  cout << *max_element(vi.begin(), vi.end()) << endl;

  return 0;
}


Fixed submission:

#include <algorithm> //min, max, sort, unique, fill
#include <iostream>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <utility> //pair, swap,
#include <vector>

#define REP(i, n) for (int i = 0; i < (n); i++)
using namespace std;
typedef long long ll;

vector<long long> vi;

int main() {
  ll n;
  ll temp;
  string s;
  cin >> n >> s;

  for (int i = 1; i < n; i++) {
    temp = 0;
    string s1 = s.substr(0, i);
    string s2 = s.substr(i, n - i);
    for (char c = 'a'; c <= 'z'; c++) {
      if (s1.find(c) != string::npos && s2.find(c) != string::npos)
        temp++;
    }
    vi.push_back(temp);
  }
  cout << *max_element(vi.begin(), vi.end()) << endl;

  return 0;
}
Problem p03338 (Python)

Buggy submission (status: Runtime Error):
n = int(input())
s = input()
ans = max(len(set(s[:i]) & set(s[i:])) for i in range(1, n - 1))
print(ans)


Fixed submission:
n = int(input())
s = input()
ans = max(len(set(s[:i]) & set(s[i:])) for i in range(1, n))
print(ans)
Problem p03338 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <array>
#include <complex>
#include <functional>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;

int main() {
  int n, ans = 0;
  string s;
  cin >> n;
  cin >> s;
  for (int i = 1; i < n; i++) {
    vector<int> x, y;
    int cnt = 0;
    for (int j = 0; j < i; j++)
      x[s[j] - 'a'] = 1;
    for (int j = i; j < n; j++)
      y[s[j] - 'a'] = 1;
    for (int j = 0; j < 26; j++) {
      if (x[j] == 1 && y[j] == 1)
        cnt++;
    }
    ans = max(ans, cnt);
  }

  cout << ans << endl;

  return 0;
}

Fixed submission:
#include <algorithm>
#include <array>
#include <complex>
#include <functional>
#include <iostream>
#include <set>
#include <string>
#include <vector>
using namespace std;
typedef unsigned long long ull;
typedef long long ll;

int main() {
  int n, ans = 0;
  string s;
  cin >> n;
  cin >> s;
  for (int i = 1; i < n; i++) {
    vector<int> x(26), y(26);
    int cnt = 0;
    for (int j = 0; j < i; j++)
      x[s[j] - 'a'] = 1;
    for (int j = i; j < n; j++)
      y[s[j] - 'a'] = 1;
    for (int j = 0; j < 26; j++) {
      if (x[j] == 1 && y[j] == 1)
        cnt++;
    }
    ans = max(ans, cnt);
  }

  cout << ans << endl;

  return 0;
}
Problem p03339 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;

void fnInput(string &rsLine) {
  int nSize;
  cin >> nSize;
  cin >> rsLine;
}

int fnAttention(const string &cnrsLine) {
  int nMinSum = INT32_MAX;
  for (int i = 0; i < cnrsLine.size(); i++) {
    int nSum = 0;
    for (int j = 0; j < cnrsLine.size(); j++) {
      if (j < i && cnrsLine[j] == 'W')
        nSum++;
      if (j > i && cnrsLine[j] == 'E')
        nSum++;
    }
    nMinSum = min(nMinSum, nSum);
  }
  return nMinSum;
}

int main() {
  string sLine;

  fnInput(sLine);
  cout << fnAttention(sLine) << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;

void fnInput(string &rsLine) {
  int nSize;
  cin >> nSize;
  cin >> rsLine;
}

int fnAttention(const string &cnrsLine) {
  int nSum = 0;
  for (int i = 1; i < cnrsLine.size(); i++)
    if (cnrsLine[i] == 'E')
      nSum++;

  int nMinSum = nSum;
  for (int i = 1; i < cnrsLine.size(); i++) {
    if (cnrsLine[i - 1] == 'W')
      nSum++;
    if (cnrsLine[i] == 'E')
      nSum--;

    nMinSum = min(nMinSum, nSum);
  }
  return nMinSum;
}

int main() {
  string sLine;

  fnInput(sLine);
  cout << fnAttention(sLine) << endl;

  return 0;
}
Problem p03339 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <iostream>
#include <string>
using namespace std;

/*A principle is memoriaization*/

int n;
string s;

int main() {
  cin >> n >> s;
  int min = 300000;
  for (int i = 0; i < n; i++) {
    int count = 0;
    for (int j = 0; j < n; j++) {
      if (i > j && s[j] == 'W') {
        count++;
      } else if (i < j && s[j] == 'E') {
        count++;
      }
    }
    if (count < min)
      min = count;
  }
  cout << min << endl;
  return 0;
}

Fixed submission:
#include <iostream>
#include <string>
using namespace std;

/*A principle is memoriaization*/

int n;
string s;

int main() {
  cin >> n >> s;
  int min = 300000;
  int cou = 0;
  for (int i = 1; i < n; i++) {
    if (s[i] == 'E')
      cou++;
  }
  min = cou;
  int prev = cou;
  for (int j = 1; j < n; j++) {
    int count = prev;
    if (s[j] == 'E')
      count--;
    if (s[j - 1] == 'W')
      count++;
    prev = count;
    if (min > count)
      min = count;
  }
  cout << min << endl;
  return 0;
}
Problem p03339 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"
using namespace std;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const long long INF = 100000000;
int dp[300100][2];

int main() {

  int N;
  cin >> N;
  string S;
  cin >> S;
  dp[0][0] = 0;
  dp[0][1] = 0;
  for (int i = 1; i < N; i++)
    if (S.at(i) == 'E')
      dp[0][1]++;
  for (int i = 0; i < N; i++) {
    if (S.at(i) == 'E')
      dp[i + 1][0] = dp[i][0];
    else
      dp[i + 1][0] = dp[i][0] + 1;
    if (S.at(i + 1) == 'E')
      dp[i + 1][1] = dp[i][1] - 1;
    else
      dp[i + 1][1] = dp[i][1];
  }

  int ans = INF;
  for (int i = 0; i < N; i++)
    chmin(ans, dp[i][0] + dp[i][1]);

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

Fixed submission:
#include "bits/stdc++.h"
using namespace std;
template <class T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}
const long long INF = 100000000;
int dp[300100][2];

int main() {

  int N;
  cin >> N;
  string S;
  cin >> S;
  dp[0][0] = 0;
  dp[0][1] = 0;
  for (int i = 1; i < N; i++)
    if (S.at(i) == 'E')
      dp[0][1]++;
  for (int i = 0; i < N - 1; i++) {
    if (S.at(i) == 'E')
      dp[i + 1][0] = dp[i][0];
    else
      dp[i + 1][0] = dp[i][0] + 1;
    if (S.at(i + 1) == 'E')
      dp[i + 1][1] = dp[i][1] - 1;
    else
      dp[i + 1][1] = dp[i][1];
  }

  int ans = INF;
  for (int i = 0; i < N; i++)
    chmin(ans, dp[i][0] + dp[i][1]);

  cout << ans << endl;
  return 0;
}
Problem p03339 (C++)

Buggy submission (status: Runtime Error):
#include "bits/stdc++.h"

using namespace std;

#define ll long long
#define vi vector<int>
#define vvi vector<vi>
#define pi pair<int, int>
#define mp make_pair
#define pb push_back
#define MOD 1e9 + 7
#define PAI 3.1415
#define all(x) (x).begin(), (x).end()
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define pr(x) cout << x << endl
#define Endl endl
#define rep(i, n) for (int i = 0; i < n; i++)

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
const int ddy[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
const int inf = 99999999;
const ll linf = 1LL << 62;

ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  if (a % b == 0)
    return b;
  else
    gcd(b, a % b);
}

ll lcm(ll a, ll b) {
  if (a < b)
    swap(a, b);
  return (a / gcd(a, b)) * b;
}

int main() {

  int n;
  string s;
  cin >> n >> s;

  vi v(100010, 0);

  // 累積和
  rep(i, n) if (s[i] == 'W') v[i] = 1;
  rep(i, n) if (i != 0) v[i] += v[i - 1];

  int ans = inf;

  rep(i, n) {
    if (i == 0)
      chmin(ans, n - 1 - (v[n - 1] - v[i]));
    else if (i == n - 1)
      chmin(ans, v[i - 1]);
    else
      chmin(ans, v[i - 1] + (n - 1 - i) - (v[n - 1] - v[i]));
  }
  cout << ans << endl;

  return 0;
}


Fixed submission:
#include "bits/stdc++.h"

using namespace std;

#define ll long long
#define vi vector<int>
#define vvi vector<vi>
#define pi pair<int, int>
#define mp make_pair
#define pb push_back
#define MOD 1e9 + 7
#define PAI 3.1415
#define all(x) (x).begin(), (x).end()
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
#define pr(x) cout << x << endl
#define Endl endl
#define rep(i, n) for (int i = 0; i < n; i++)

const int dx[4] = {1, 0, -1, 0};
const int dy[4] = {0, 1, 0, -1};
const int ddx[8] = {-1, 0, 1, -1, 1, -1, 0, 1};
const int ddy[8] = {-1, -1, -1, 0, 0, 1, 1, 1};
const int inf = 99999999;
const ll linf = 1LL << 62;

ll gcd(ll a, ll b) {
  if (a < b)
    swap(a, b);
  if (a % b == 0)
    return b;
  else
    gcd(b, a % b);
}

ll lcm(ll a, ll b) {
  if (a < b)
    swap(a, b);
  return (a / gcd(a, b)) * b;
}

int main() {

  int n;
  string s;
  cin >> n >> s;

  vi v(3 * 100010, 0);

  // 累積和
  rep(i, n) if (s[i] == 'W') v[i] = 1;
  rep(i, n) if (i != 0) v[i] += v[i - 1];

  int ans = inf;

  rep(i, n) {
    if (i == 0)
      chmin(ans, n - 1 - (v[n - 1] - v[i]));
    else if (i == n - 1)
      chmin(ans, v[i - 1]);
    else
      chmin(ans, v[i - 1] + (n - 1 - i) - (v[n - 1] - v[i]));
  }
  cout << ans << endl;

  return 0;
}
Problem p03339 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <climits>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
  int n;
  string s;
  cin >> n;
  cin >> s;
  vector<bool> v(s.length());
  for (int i = 0; i < s.length(); ++i)
    if (s[i] == 'E')
      v[i] = 0;
    else
      v[i] = 1;
  int mc = INT_MAX;
  for (int i = 0; i < v.size(); ++i) {
    int c = 0;
    for (int j = 0; j < i; ++j) {
      if (v[j])
        ++c;
    }
    for (int j = i + 1; j < v.size(); ++j)
      if (!v[j])
        ++c;
    mc = min(mc, c);
  }
  cout << mc << endl;
  return 0;
}

Fixed submission:
#include <algorithm>
#include <climits>
#include <cstring>
#include <iostream>
#include <string>
#include <vector>
using namespace std;

int main() {
  int n;
  string s;
  cin >> n;
  cin >> s;
  vector<bool> v(s.length());
  for (int i = 0; i < s.length(); ++i)
    if (s[i] == 'E')
      v[i] = 0;
    else
      v[i] = 1;
  int mc, lc = 0, rc = 0;
  for (int i = 1; i < v.size(); ++i)
    if (!v[i])
      ++rc;
  mc = rc;
  for (int i = 1; i < v.size(); ++i) {
    if (v[i - 1])
      ++lc;
    if (!v[i] && rc > 0)
      --rc;
    mc = min(mc, lc + rc);
  }
  cout << mc << endl;
  return 0;
}
Problem p03339 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;
int n, res = 1e9, e[100005], w[100005];
string s;
int main() {
  cin >> n;
  cin >> s;
  s = '0' + s;
  for (int i = 1; i < s.size(); i++) {
    if (s[i] == 'W') {
      e[i] = e[i - 1] + 1;
    } else
      e[i] = e[i - 1];
  }
  for (int i = n; i >= 1; i--) {
    if (s[i] == 'E') {
      w[i] = w[i + 1] + 1;
    } else
      w[i] = w[i + 1];
  }
  for (int i = 1; i < s.size(); i++) {
    int t1 = e[i - 1];
    int t2 = w[i + 1];
    if (t1 + t2 < res)
      res = t1 + t2;
  } // WEWEWEEEWWWE
  cout << res;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>

using namespace std;
int n, res = 1e9, e[300005], w[300005];
string s;
int main() {
  cin >> n;
  cin >> s;
  s = '0' + s;
  for (int i = 1; i < s.size(); i++) {
    if (s[i] == 'W') {
      e[i] = e[i - 1] + 1;
    } else
      e[i] = e[i - 1];
  }
  for (int i = n; i >= 1; i--) {
    if (s[i] == 'E') {
      w[i] = w[i + 1] + 1;
    } else
      w[i] = w[i + 1];
  }
  for (int i = 1; i < s.size(); i++) {
    int t1 = e[i - 1];
    int t2 = w[i + 1];
    if (t1 + t2 < res)
      res = t1 + t2;
  } // WEWEWEEEWWWE
  cout << res;
  return 0;
}
Problem p03339 (C++)

Buggy submission (status: Runtime Error):
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>

typedef long long ll;

#define rep(i, n) for (ll i = 0; i < n; i++)

using namespace std;

const ll INF = ll(1e18) + 1;
const double PI = 3.14159265358979323846;

int main() {
  int N;
  string S;
  cin >> N >> S;

  int A[10010] = {};
  for (int i = 1; i <= N; i++) {
    if (S[i - 1] == 'E')
      A[i] = A[i - 1] + 1;
    else
      A[i] = A[i - 1];
  }
  int ans = 1e+9;
  for (int i = 1; i <= N; i++) {
    ans = min(ans, A[N] - A[i] + i - 1 - A[i - 1]);
  }
  cout << ans << endl;
}


Fixed submission:
#include <algorithm>
#include <cmath>
#include <iostream>
#include <queue>
#include <stdio.h>
#include <string>
#include <utility>
#include <vector>

typedef long long ll;

#define rep(i, n) for (ll i = 0; i < n; i++)

using namespace std;

const ll INF = ll(1e18) + 1;
const double PI = 3.14159265358979323846;

int main() {
  int N;
  string S;
  cin >> N >> S;

  int A[300010] = {};
  for (int i = 1; i <= N; i++) {
    if (S[i - 1] == 'E')
      A[i] = A[i - 1] + 1;
    else
      A[i] = A[i - 1];
  }
  int ans = 1e+9;
  for (int i = 1; i <= N; i++) {
    ans = min(ans, A[N] - A[i] + i - 1 - A[i - 1]);
  }
  cout << ans << endl;
}
Problem p03339 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define repr(i, n) for (int(i) = (n); (i) >= 0; (i)--)
#define FOR(i, m, n) for (int(i) = (m); (i) < (n); (i)++)
#define all(x) (x).begin(), (x).end()
typedef long long lint;
using namespace std;

int main(void) {

  int n;
  int count = 0;
  int min = 500000;
  string s;
  int w[30001] = {0};
  int e[30001] = {0};
  cin >> n >> s;

  rep(i, n) {
    if (i != 0) {
      if (s[i - 1] == 'W') {
        w[i] = w[i - 1] + 1;
      } else {
        w[i] = w[i - 1];
      }
      if (s[((n - 1) - i) + 1] == 'E') {
        e[(n - 1) - i] = e[((n - 1) - i) + 1] + 1;
      } else {
        e[(n - 1) - i] = e[((n - 1) - i) + 1];
      }
    }
  }
  rep(i, n) {
    if (w[i] + e[i] < min) {
      min = w[i] + e[i];
    }
  }

  cout << min << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define rep(i, n) for (int(i) = 0; (i) < (n); (i)++)
#define repr(i, n) for (int(i) = (n); (i) >= 0; (i)--)
#define FOR(i, m, n) for (int(i) = (m); (i) < (n); (i)++)
#define all(x) (x).begin(), (x).end()
typedef long long lint;
using namespace std;

int main(void) {

  int n;
  int count = 0;
  int min = 500000;
  string s;
  int w[300000] = {0};
  int e[300000] = {0};
  cin >> n >> s;

  rep(i, n) {
    if (i != 0) {
      if (s[i - 1] == 'W') {
        w[i] = w[i - 1] + 1;
      } else {
        w[i] = w[i - 1];
      }
      if (s[((n - 1) - i) + 1] == 'E') {
        e[(n - 1) - i] = e[((n - 1) - i) + 1] + 1;
      } else {
        e[(n - 1) - i] = e[((n - 1) - i) + 1];
      }
    }
  }
  rep(i, n) {
    if (w[i] + e[i] < min) {
      min = w[i] + e[i];
    }
  }

  cout << min << endl;
  return 0;
}
Problem p03339 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int maxn = 2e5 + 7;
int w[maxn], e[maxn];
int main() {
  int n;
  cin >> n;
  string str;
  cin >> str;
  memset(e, 0, sizeof(e));
  memset(w, 0, sizeof(w));
  if (str[0] == 'W')
    w[0] = 1;
  for (int i = 1; i < n; i++) {
    if (str[i] == 'W')
      w[i] = w[i - 1] + 1;
    else
      w[i] = w[i - 1];
  }

  if (str[n - 1] == 'E')
    e[n - 1] = 1;
  for (int i = n - 2; i >= 0; i--) {
    if (str[i] == 'E')
      e[i] = e[i + 1] + 1;
    else
      e[i] = e[i + 1];
  }
  int ans = 100000000;
  ans = min(e[1], ans);
  // cout<<ans<<endl;
  for (int i = 1; i < n; i++)
    ans = min(e[i + 1] + w[i - 1], ans); // cout<<i<<" "<<ans<<endl;
  cout << ans << endl;
  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
#define ll long long
using namespace std;

const int maxn = 2e6 + 7;
int w[maxn], e[maxn];
int main() {
  int n;
  cin >> n;
  string str;
  cin >> str;
  memset(e, 0, sizeof(e));
  memset(w, 0, sizeof(w));
  if (str[0] == 'W')
    w[0] = 1;
  for (int i = 1; i < n; i++) {
    if (str[i] == 'W')
      w[i] = w[i - 1] + 1;
    else
      w[i] = w[i - 1];
  }

  if (str[n - 1] == 'E')
    e[n - 1] = 1;
  for (int i = n - 2; i >= 0; i--) {
    if (str[i] == 'E')
      e[i] = e[i + 1] + 1;
    else
      e[i] = e[i + 1];
  }
  int ans = 100000000;
  ans = min(e[1], ans);
  // cout<<ans<<endl;
  for (int i = 1; i < n; i++)
    ans = min(e[i + 1] + w[i - 1], ans); // cout<<i<<" "<<ans<<endl;
  cout << ans << endl;
  return 0;
}
Problem p03339 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
char a[150];
int b[3][3], r, n;
int main() {
  scanf("%d%*c", &n);
  scanf("%s", &a);
  r = 1 << 24;
  for (int i = 0; i < n; i++) {
    if (a[i] == 'E')
      b[0][0]++;
    else
      b[0][1]++;
  }
  for (int i = n - 1, k; i >= -1; i--) {
    k = b[0][1] + b[1][0];
    if (k < r)
      r = k;
    if (i >= 0 && a[i] == 'E') {
      b[0][0]--;
      b[1][0]++;
    } else {
      b[0][1]--;
      b[1][1]++;
    }
  }
  printf("%d", r);
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
char a[350000];
int b[3][3], r, n;
int main() {
  scanf("%d%*c", &n);
  scanf("%s", &a);
  r = 1 << 24;
  for (int i = 0; i < n; i++) {
    if (a[i] == 'E')
      b[0][0]++;
    else
      b[0][1]++;
  }
  for (int i = n - 1, k; i >= -1; i--) {
    k = b[0][1] + b[1][0];
    if (k < r)
      r = k;
    if (i >= 0 && a[i] == 'E') {
      b[0][0]--;
      b[1][0]++;
    } else {
      b[0][1]--;
      b[1][1]++;
    }
  }
  printf("%d", r);
  return 0;
}
Problem p03339 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>
#include <math.h>
using namespace std;
#define MAX (30 + 2)

int main(int argc, const char *argv[]) {
  int N, l[MAX] = {0}, r[MAX] = {0};
  string s;
  cin >> N >> s;
  for (int i = 1; i < N - 1; i++) {
    if (s[i - 1] == 'W') {
      l[i]++;
    }
    l[i + 1] += l[i];
  }
  for (int i = N - 1; i > 0; i--) {
    if (s[i + 1] == 'E') {
      r[i]++;
    }
    r[i - 1] = r[i];
  }
  int MIN = N;
  for (int i = 0; i < N; i++) {
    MIN = min(MIN, l[i] + r[i]);
  }
  cout << MIN << endl;
}


Fixed submission:
#include <iostream>
#include <math.h>
using namespace std;
#define MAX (300000 + 2)

int main(int argc, const char *argv[]) {
  int N, l[MAX] = {0}, r[MAX] = {0};
  string s;
  cin >> N >> s;
  for (int i = 1; i < N - 1; i++) {
    if (s[i - 1] == 'W') {
      l[i]++;
    }
    l[i + 1] += l[i];
  }
  for (int i = N - 1; i > 0; i--) {
    if (s[i + 1] == 'E') {
      r[i]++;
    }
    r[i - 1] = r[i];
  }
  int MIN = N;
  for (int i = 0; i < N; i++) {
    MIN = min(MIN, l[i] + r[i]);
  }
  cout << MIN << endl;
}
Problem p03339 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
/// Welcome to Nasif's Code
#define bug printf("bug\n");
#define bug2(var) cout << #var << " " << var << endl;
#define co(q) cout << q << endl;
typedef long long int ll;
typedef unsigned long long int ull;
const int MOD = (int)1e9 + 7;
const int MAX = 1e6;
#define pi acos(-1)
#define inf 1000000000000000LL
#define FastRead                                                               \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
int pre_e[200], pre_w[200], suf_w[200], suf_e[200];
int main() {
  FastRead
      // freopen("output.txt", "w", stdout);

      int len;
  string s;
  cin >> len;
  cin >> s;
  for (int i = 1; i <= len; i++) {
    pre_e[i] = pre_e[i - 1];
    pre_w[i] = pre_w[i - 1];
    if (s[i - 1] == 'E')
      pre_e[i]++;
    else
      pre_w[i]++;
  }
  for (int i = len; i >= 1; i--) {
    suf_e[i] = suf_e[i + 1];
    suf_w[i] = suf_w[i + 1];
    if (s[i - 1] == 'E')
      suf_e[i]++;
    else
      suf_w[i]++;
  }
  int mx = INT_MAX;
  for (int i = 1; i <= len; i++) {
    int x = pre_w[i - 1] + suf_e[i + 1];
    mx = min(x, mx);
  }
  cout << mx << endl;

  return 0;
}


Fixed submission:
#include <bits/stdc++.h>
using namespace std;
/// Welcome to Nasif's Code
#define bug printf("bug\n");
#define bug2(var) cout << #var << " " << var << endl;
#define co(q) cout << q << endl;
typedef long long int ll;
typedef unsigned long long int ull;
const int MOD = (int)1e9 + 7;
const int MAX = 1e6;
#define pi acos(-1)
#define inf 1000000000000000LL
#define FastRead                                                               \
  ios::sync_with_stdio(0);                                                     \
  cin.tie(0);                                                                  \
  cout.tie(0);
int pre_e[MAX], pre_w[MAX], suf_w[MAX], suf_e[MAX];
int main() {
  FastRead
      // freopen("output.txt", "w", stdout);

      int len;
  string s;
  cin >> len;
  cin >> s;
  for (int i = 1; i <= len; i++) {
    pre_e[i] = pre_e[i - 1];
    pre_w[i] = pre_w[i - 1];
    if (s[i - 1] == 'E')
      pre_e[i]++;
    else
      pre_w[i]++;
  }
  for (int i = len; i >= 1; i--) {
    suf_e[i] = suf_e[i + 1];
    suf_w[i] = suf_w[i + 1];
    if (s[i - 1] == 'E')
      suf_e[i]++;
    else
      suf_w[i]++;
  }
  int mx = INT_MAX;
  for (int i = 1; i <= len; i++) {
    int x = pre_w[i - 1] + suf_e[i + 1];
    mx = min(x, mx);
  }
  cout << mx << endl;

  return 0;
}
Problem p03339 (C++)

Buggy submission (status: Runtime Error):
#include <iostream>

using namespace std;

int main() {
  long n;
  char *s;
  s = new char[n];
  cin >> n;
  cin >> s;

  int tmp_min, min = 0;
  int x, y = 0;
  for (long i = 1; i < n; i++) {
    if (s[i] != 'W')
      y++;
  }
  tmp_min = y;
  min = y;
  //	cout << "first: " << tmp_min << endl;
  char pre_L = s[0];
  char L;
  for (long i = 1; i < n; i++) {
    L = s[i];
    if (pre_L == 'W')
      tmp_min++;
    if (L == 'E')
      tmp_min--;
    pre_L = L;
    //		cout << "tmp: " << tmp_min  <<"  "<<min<< endl;
    if (min > tmp_min)
      min = tmp_min;
  }

  cout << min;
  return 0;
}

Fixed submission:
#include <iostream>

using namespace std;

int main() {
  long n;
  string s;
  cin >> n;
  cin >> s;

  int tmp_min, min = 0;
  int x, y = 0;
  for (long i = 1; i < n; i++) {
    if (s[i] != 'W')
      y++;
  }
  tmp_min = y;
  min = y;
  //	cout << "first: " << tmp_min << endl;
  char pre_L = s[0];
  char L;
  for (long i = 1; i < n; i++) {
    L = s[i];
    if (pre_L == 'W')
      tmp_min++;
    if (L == 'E')
      tmp_min--;
    pre_L = L;
    //		cout << "tmp: " << tmp_min  <<"  "<<min<< endl;
    if (min > tmp_min)
      min = tmp_min;
  }

  cout << min;
  return 0;
}
Problem p03339 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  scanf("%d", &N);
  char s[N];
  scanf("%s", s);
  int i, j, cnt;
  vector<int> d(N);
  for (i = 0; i < N; i++) {
    cnt = 0;
    for (j = 0; j < i; j++)
      if (s[j] == 'W')
        cnt++;
    for (j = i + 1; j < N; j++)
      if (s[j] == 'E')
        cnt++;
    d[i] = cnt;
  }
  printf("%d\n", *min_element(d.begin(), d.end()));
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int main() {
  int N;
  scanf("%d", &N);
  char s[N];
  scanf("%s", s);
  int i, j, c, p;
  vector<int> d(N), e(N), w(N);
  w[0] = 0;
  for (i = 1; i < N; i++)
    w[i] = w[i - 1] + (s[i - 1] == 'W');
  e[N - 1] = 0;
  for (i = N - 2; i >= 0; i--)
    e[i] = e[i + 1] + (s[i + 1] == 'E');
  for (i = 0; i < N; i++)
    w[i] += e[i];
  printf("%d\n", *min_element(w.begin(), w.end()));
}
Problem p03339 (C++)

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

using namespace std;

int main() {
  int n;
  string s;

  cin >> n >> s;

  vector<int> num_E{s[0] == 'E'};

  for (int i = 1; i < s.size(); i++) {
    num_E[i] = num_E[i - 1] + (s[i] == 'E');
  }

  int ans = num_E[s.size() - 1] - num_E[0];

  for (int i = 1; i < s.size(); i++) {
    ans = min(ans, num_E[s.size() - 1] - num_E[i] - num_E[i - 1] + i);
  }

  cout << ans << endl;
}


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

using namespace std;

int main() {
  int n;
  string s;

  cin >> n >> s;

  vector<int> num_E(n);

  num_E[0] = (s[0] == 'E');

  for (int i = 1; i < s.size(); i++) {
    num_E[i] = num_E[i - 1] + (s[i] == 'E');
  }

  int ans = num_E[s.size() - 1] - num_E[0];

  for (int i = 1; i < s.size(); i++) {
    ans = min(ans, num_E[s.size() - 1] - num_E[i] - num_E[i - 1] + i);
  }

  cout << ans << endl;
}
Problem p03339 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>
using namespace std;
int w[100001], e[100001];
int main() {
  int N, min = 3333333, ans = 0, p, l, r;
  string S;
  cin >> N >> S;
  if (S[0] == 'W')
    w[0] = 1;
  else
    e[0] = 1;
  for (p = 1; p < N; p++) {
    if (S[p] == 'W')
      w[p] = w[p - 1] + 1;
    else
      w[p] = w[p - 1];
    if (S[p] == 'E')
      e[p] = e[p - 1] + 1;
    else
      e[p] = e[p - 1];
  }
  /*
  for(p=0;p!=N;p++){
          cout<<w[p]<<" ";
  }
  cout<<endl;
  for(p=0;p!=N;p++){
          cout<<e[p]<<" ";
  }
  */
  //--------------------
  for (p = 0; p < N; p++) {
    if (p == 0) {
      l = 0;
      r = e[N - 1] - e[p];
    }
    if (p == N - 1) {
      l = w[N - 2];
      r = 0;
    }
    if (p != 0 && p != N - 1) {
      l = w[p - 1];
      r = e[N - 1] - e[p];
    }
    ans = l + r;
    if (ans < min)
      min = ans;
  }
  cout << min;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
using namespace std;
int w[300001], e[300001];
int main() {
  int N, min = 3333333, ans = 0, p, l, r;
  string S;
  cin >> N >> S;
  if (S[0] == 'W')
    w[0] = 1;
  else
    e[0] = 1;
  for (p = 1; p < N; p++) {
    if (S[p] == 'W')
      w[p] = w[p - 1] + 1;
    else
      w[p] = w[p - 1];
    if (S[p] == 'E')
      e[p] = e[p - 1] + 1;
    else
      e[p] = e[p - 1];
  }
  /*
  for(p=0;p!=N;p++){
          cout<<w[p]<<" ";
  }
  cout<<endl;
  for(p=0;p!=N;p++){
          cout<<e[p]<<" ";
  }
  */
  //--------------------
  for (p = 0; p < N; p++) {
    if (p == 0) {
      l = 0;
      r = e[N - 1] - e[p];
    }
    if (p == N - 1) {
      l = w[N - 2];
      r = 0;
    }
    if (p != 0 && p != N - 1) {
      l = w[p - 1];
      r = e[N - 1] - e[p];
    }
    ans = l + r;
    if (ans < min)
      min = ans;
  }
  cout << min;
  return 0;
}