█████████ ████ ███░░░░░███ ░░███ ███ ░░░ ██████ ███████ ██████ ██████ ░███ ███░░███ ███░░███ ███░░███ ███░░███ ░███ ░███ ░███░███ ░███ ░███████ ░███ ░███ ░░███ ███░███ ░███░███ ░███ ░███░░░ ░███ ░███ ░░█████████ ░░██████ ░░████████░░██████ ░░██████ ░░░░░░░░░ ░░░░░░ ░░░░░░░░ ░░░░░░ ░░░░░░

Envío 2182

Problema 0x59 - Substring más larga con máximo K caracteres diferentes

  • Autor: Mejibyte
  • Fecha: 2020-11-27 22:26:25 UTC (Hace más de 3 años)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.006 s 1 KBi
#2
Correcto
0.007 s 1 KBi
#3
Correcto
0.007 s 1 KBi
#4
Correcto
0.008 s 1 KBi
#5
Correcto
0.006 s 1 KBi
#6
Correcto
0.005 s 1 KBi
#7
Correcto
0.007 s 1 KBi
#8
Correcto
0.006 s 1 KBi
#9
Correcto
0.007 s 1 KBi
#10
Tiempo límite excedido
1.007 s 1 KBi
#11
Correcto
0.097 s 1 KBi
#12
Tiempo límite excedido
1.067 s 2 KBi
#13
Correcto
0.203 s 1 KBi
#14
Tiempo límite excedido
1.024 s 2 KBi
#15
Tiempo límite excedido
1.014 s 1 KBi
#16
Correcto
0.205 s 1 KBi
#17
Tiempo límite excedido
0.853 s 1 KBi
#18
Correcto
0.121 s 1 KBi
#19
Tiempo límite excedido
1.02 s 1 KBi
#20
Tiempo límite excedido
1.018 s 1 KBi
#21
Tiempo límite excedido
1.005 s 1 KBi
#22
Tiempo límite excedido
1.033 s 1 KBi
#23
Tiempo límite excedido
1.032 s 1 KBi
#24
Tiempo límite excedido
1.024 s 1 KBi
Puntos totales: 55 / 100

Código

#include <iostream>
#include <vector>
#include <cassert>
#include <set>
#include <unordered_set>
using namespace std;

int main() {
  string s;
  cin >> s;
  int n = s.size();
  assert(1 <= n and n <= 100000);
  int k;
  cin >> k;
  assert(1 <= k and k <= 26);

  int best = -1;
  for (int i = 0; i < n; i++) {
    unordered_set<char> seen;
    for (int j = i; j < n; j++) {
      seen.insert(s[j]);
      if (seen.size() > k) {
        break;
      }

      best = max(best, j - i + 1);
    }
  }
  cout << best << endl;
  return 0;
}