#include <bits/stdc++.h> #define N 200000 using namespace std; string s; int a[N+3],…
- User
#include <bits/stdc++.h> #define N 200000 using namespace std; string s; int a[N+3], nho[N+3]; int ans=0; int tknp(int x, int d, int c){ int p = -1; while (d <= c){ int mid = (d+ c) /2; if (a[nho[mid]] < x) { p = nho[mid]; c = mid - 1; } else d = mid + 1; } return p; } int main() { ios_base::sync_with_stdio(0); cin.tie(0);cout.tie(0); freopen("matdo.inp", "r", stdin); freopen("matdo.out", "w", stdout); cin >> s; int n = s.size(); for (char x = 'a'; x <= 'z'; x++) { memset(a,0,sizeof(a)); memset(nho, 0, sizeof(nho)); for (int i=0; i<s.size(); i++) if (s[i] == x) a[i+1] = a[i] + 1; else a[i+1] = a[i] - 1; for (int i=1; i<=n; i++) if (a[i] < a[nho[i-1]]) nho[i] = i; else nho[i] = nho[i-1]; for (int i=n; i>0; i--) { int j = tknp (a[i],0,i-1); if (j>-1) ans = max(ans,i-j); } } cout << ans; return 0; } dịch nghĩa tieng việt
- Assistant
Chương trình trên là một đoạn mã C++ giải quyết một bài toán liên quan đến chuỗi ký tự. Dưới đây là phần giải thích từng phần của mã, và sau đó là dịch nghĩa thuật toán sang tiếng Việt.