fork(1) download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. // Speed
  4. #define fast_io ios::sync_with_stdio(0); cin.tie(0); cout.tie(0)
  5. // Typedefs
  6. #define int long long
  7. #define pb push_back
  8. #define ff first
  9. #define ss second
  10. #define all(x) (x).begin(), (x).end()
  11. #define rall(x) (x).rbegin(), (x).rend()
  12. #define sz(x) ((int)(x).size())
  13. #define endl '\n'
  14. #define yes cout << "yes\n"
  15. #define no cout << "no\n"
  16. // Loops
  17. #define rep(i,a,b) for(int i=a;i<b;++i)
  18. #define per(i,a,b) for(int i=b-1;i>=a;--i)
  19. #define each(x, a) for (auto& x : a)
  20. // Consts
  21. const int INF = 1e18;
  22. const int MOD = 1e9+7;
  23. const int N = 2e5 + 5;
  24. // Math
  25. int gcd(int a, int b) { return b == 0 ? a : gcd(b, a % b); }
  26. int lcm(int a, int b) { return (a / gcd(a, b)) * b; }
  27. int power(int a, int b, int m = MOD) {
  28. int res = 1;
  29. while (b > 0) {
  30. if (b & 1) res = res * a % m;
  31. a = a * a % m;
  32. b >>= 1;
  33. }
  34. return res;
  35. }
  36. int modinv(int a, int m = MOD) {
  37. return power(a, m - 2, m);
  38. }
  39.  
  40. // Logic
  41. void solve() {
  42. int n;
  43. cin >> n;
  44. string s;
  45. cin >> s;
  46.  
  47. if (n < 4) {
  48. cout << 0 << endl;
  49. return;
  50. }
  51.  
  52. int ans1 = INF;
  53. rep(i, 0, n - 3) {
  54. int cost = 0;
  55. string target = "2026";
  56. rep(j, 0, 4) {
  57. if (s[i + j] != target[j]) cost++;
  58. }
  59. ans1 = min(ans1, cost);
  60. }
  61. int ans2 = 0;
  62. rep(i, 0, n - 3) {
  63. if (s.substr(i, 4) == "2025") {
  64. ans2++;
  65. }
  66. }
  67.  
  68. cout << min(ans1, ans2) << endl;
  69. }
  70.  
  71. // Main
  72. int32_t main() {
  73. fast_io;
  74. int t;
  75. cin >> t;
  76. while (t--) {
  77. solve();
  78. }
  79. return 0;
  80. }
Success #stdin #stdout 0.01s 5288KB
stdin
7
4
0000
4
2025
4
2026
8
20252026
8
20252025
9
202520256
9
202520265
stdout
0
1
0
0
1
1
0