fork download
  1. // em thu thoi
  2. #include <bits/stdc++.h>
  3.  
  4. using namespace std;
  5.  
  6. #define int long long
  7. #define ld long double
  8. #define eb emplace_back
  9. #define pb push_back
  10. #define fi first
  11. #define se second
  12. #define nn '\n'
  13. #define pi pair<int, int>
  14. #define unmp unordered_map
  15. #define uns unordered_set
  16. #define lb lower_bound
  17. #define ub upper_bound
  18. #define TASK " "
  19.  
  20. #define FOR(i, a, n) for(int i = (a) ; i <= (n) ; i++ )
  21. #define FORD(i, a, n) for(int i = (a); i >= (n); i--)
  22. #define FORX(i, x) for(auto (i) : (x))
  23. #define ms(a, x) memset((a), (x), sizeof((a)))
  24. #define all(a) (a).begin(), (a).end()
  25. #define All(a, n) (a) + 1, (a) + 1 + n
  26.  
  27.  
  28. const int INF = 1e18;
  29. const int mod = 1e9+7;
  30. const int N = 1e6 + 5;
  31. int n;
  32. char a[N];
  33. map<char, int> cnt;
  34. int mx = 0;
  35. int mn = INF;
  36. int kq = 0;
  37. vector<int> pos[26];
  38. void nhap(){
  39. cin >> n;
  40. FOR(i, 1, n){
  41. cin >> a[i];
  42. pos[a[i] - 'a'].pb(i);
  43. }
  44. }
  45. void solve(){
  46. for(int i = 0; i <= 25; i++){
  47. if(pos[i].empty()) continue;
  48. for(int j = 0; j <= 25; j++){
  49. // gia su i la max, j la min
  50. if(i == j || pos[j].empty()) continue;
  51. int cnti = 0, cntj = -1000000000;
  52. int u = 0, v = 0;
  53. while(u < (int)pos[i].size() || v < (int)pos[j].size()){
  54. if(u < (int)pos[i].size() && (v == (int)pos[j].size() || pos[i][u] < pos[j][v])){
  55. cnti++;
  56. if(cntj != -1000000000){
  57. cntj++;
  58. }
  59. u++;
  60. }
  61.  
  62. else{
  63. cntj = max(cnti - 1, (cntj != -1000000000 ? cntj - 1 : -1000000000));
  64. cnti = 0;
  65. v++;
  66. }
  67.  
  68. if(cntj != -1000000000){
  69. kq = max(kq, cntj);
  70. }
  71. }
  72.  
  73. }
  74. }
  75. cout << kq << nn;
  76. }
  77. signed main() {
  78. //freopen("CSBN.INP", "r", stdin);
  79. //freopen("CSBN.OUT", "w", stdout);
  80. ios_base::sync_with_stdio(0);
  81. cin.tie(0);
  82. cout.tie(0);
  83. nhap();
  84. solve();
  85. return 0;
  86. }
Success #stdin #stdout 0.01s 5308KB
stdin
4
aaba
stdout
2