fork download
  1. #include <bits/stdc++.h>
  2.  
  3. #define debug cout << "ok\n";
  4. #define SQR(x) (1LL * ((x) * (x)))
  5. #define MASK(i) (1LL << (i))
  6. #define BIT(x, i) (((x) >> (i)) & 1)
  7. #define fi first
  8. #define se second
  9. #define pb push_back
  10.  
  11. #define mp make_pair
  12. #define pii pair<int,int>
  13. #define pli pair<ll,int>
  14. #define vi vector<int>
  15.  
  16. #define FAST ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
  17.  
  18. typedef long long ll;
  19. typedef unsigned long long ull;
  20. typedef long double ld;
  21. typedef unsigned int ui;
  22.  
  23. using namespace std;
  24.  
  25. const int M = 1e9 + 7;
  26. const int INF = 1e9 + 7;
  27. const ll INFLL = (ll)2e18 + 7LL;
  28. const ld PI = acos(-1);
  29.  
  30. const int dx[] = {1, -1, 0, 0, -1, 1, 1, -1};
  31. const int dy[] = {0, 0, 1, -1, -1, -1, 1, 1};
  32.  
  33. template<class _, class __>
  34. bool minimize(_ &x, const __ y){
  35. if(x > y){
  36. x = y;
  37. return true;
  38. } else return false;
  39. }
  40. template<class _, class __>
  41. bool maximize(_ &x, const __ y){
  42. if(x < y){
  43. x = y;
  44. return true;
  45. } else return false;
  46. }
  47.  
  48. template<class _,class __>
  49. void Add(_ &x, const __ y) {
  50. x += y;
  51. if (x >= M) {
  52. x -= M;
  53. }
  54. return;
  55. }
  56.  
  57. template<class _,class __>
  58. void Diff(_ &x, const __ y) {
  59. x -= y;
  60. if (x < 0) {
  61. x += M;
  62. }
  63. return;
  64. }
  65.  
  66. //--------------------------------------------------------------
  67.  
  68. const int MaxN = 1e6+7;
  69.  
  70. int n,cnt[26],f[MaxN][26][2],g[26][26],bf[26],a[MaxN];
  71.  
  72. void sol() {
  73. cin >> n;
  74. for (int i=1;i<=n;i++) {
  75. char x;
  76. cin >> x;
  77. a[i] = x - 'a';
  78. }
  79. int res = 0;
  80. memset(cnt,0,sizeof(cnt));
  81. for (int i=1;i<=n;i++) {
  82. cnt[a[i]]++;
  83. bf[a[i]] = i;
  84. for (int j=0;j<26;j++) {
  85. if (bf[j]) {
  86. maximize(res,cnt[a[i]] - cnt[j] - f[bf[j]][a[i]][1]);
  87. maximize(res,cnt[j] - cnt[a[i]] - f[bf[j]][a[i]][0]);
  88. }
  89. }
  90.  
  91. for (int j=0;j<26;j++) {
  92. minimize(g[a[i]][j],(cnt[a[i]] - 1) - (cnt[j] - (a[i] == j)));
  93. minimize(g[j][a[i]],(cnt[j] - (a[i] == j)) - (cnt[a[i]]-1));
  94. f[i][j][0] = g[a[i]][j];
  95. f[i][j][1] = g[j][a[i]];
  96. }
  97. }
  98. cout << res;
  99. }
  100.  
  101. int main() {
  102. // freopen("test.inp","r",stdin);
  103. // freopen("test.out","w",stdout);
  104. FAST
  105. int t=1;
  106. // cout << sizeof(f) << '\n';
  107. // cin >> t;
  108. while (t--) sol();
  109. }
  110.  
Success #stdin #stdout 0.01s 5320KB
stdin
Standard input is empty
stdout
Standard output is empty