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

Envío 7165

Problema 0x43 - Encontrar el borde más largo de una string

  • Autor: Ikerlb
  • Fecha: 2023-10-05 02:32:36 UTC (Hace alrededor de 1 año)
Caso # Resultado Tiempo Memoria
#1
Correcto
0.144 s 17 KBi
#2
Correcto
0.11 s 16 KBi
#3
Correcto
0.137 s 17 KBi
#4
Incorrecto
0.129 s 17 KBi
#5
Error en tiempo de ejecución (NZEC)
Exited with error status 1
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 100010
	at Main.getBorder(Main.java:117)
	at Main.run(Main.java:140)
	at Main.main(Main.java:157)
0.127 s 17 KBi
#6
Error en tiempo de ejecución (NZEC)
Exited with error status 1
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index -1 out of bounds for length 100010
	at Main.getBorder(Main.java:117)
	at Main.run(Main.java:140)
	at Main.main(Main.java:157)
0.104 s 16 KBi
#7
Error en tiempo de ejecución (NZEC)
Exited with error status 1
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 100014 out of bounds for length 100010
	at Main.constructSA(Main.java:42)
	at Main.run(Main.java:137)
	at Main.main(Main.java:157)
0.333 s 23 KBi
#8
Error en tiempo de ejecución (NZEC)
Exited with error status 1
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 100010 out of bounds for length 100010
	at Main.constructSA(Main.java:42)
	at Main.run(Main.java:137)
	at Main.main(Main.java:157)
0.348 s 24 KBi
#9
Error en tiempo de ejecución (NZEC)
Exited with error status 1
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 100011 out of bounds for length 100010
	at Main.constructSA(Main.java:42)
	at Main.run(Main.java:137)
	at Main.main(Main.java:157)
0.407 s 22 KBi
#10
Error en tiempo de ejecución (NZEC)
Exited with error status 1
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 100011 out of bounds for length 100010
	at Main.constructSA(Main.java:42)
	at Main.run(Main.java:137)
	at Main.main(Main.java:157)
0.348 s 24 KBi
#11
Correcto
0.444 s 22 KBi
#12
Error en tiempo de ejecución (NZEC)
Exited with error status 1
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 100012 out of bounds for length 100010
	at Main.constructSA(Main.java:42)
	at Main.run(Main.java:137)
	at Main.main(Main.java:157)
0.448 s 23 KBi
#13
Error en tiempo de ejecución (NZEC)
Exited with error status 1
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 100013 out of bounds for length 100010
	at Main.constructSA(Main.java:42)
	at Main.run(Main.java:137)
	at Main.main(Main.java:157)
0.366 s 21 KBi
#14
Error en tiempo de ejecución (NZEC)
Exited with error status 1
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 100013 out of bounds for length 100010
	at Main.constructSA(Main.java:42)
	at Main.run(Main.java:137)
	at Main.main(Main.java:157)
0.371 s 24 KBi
#15
Error en tiempo de ejecución (NZEC)
Exited with error status 1
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 100013 out of bounds for length 100010
	at Main.constructSA(Main.java:42)
	at Main.run(Main.java:137)
	at Main.main(Main.java:157)
0.345 s 21 KBi
#16
Correcto
0.526 s 23 KBi
#17
Correcto
0.783 s 23 KBi
#18
Correcto
0.648 s 24 KBi
#19
Incorrecto
0.709 s 22 KBi
#20
Incorrecto
0.647 s 22 KBi
#21
Correcto
0.629 s 23 KBi
#22
Error en tiempo de ejecución (NZEC)
Exited with error status 1
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 100014 out of bounds for length 100010
	at Main.constructSA(Main.java:42)
	at Main.run(Main.java:137)
	at Main.main(Main.java:157)
0.4 s 23 KBi
Puntos totales: 37 / 100

Código

import java.util.*;

class Main {
  char T[];
  int n;  

  int[] RA, tempRA;
  Integer[] SA, tempSA;
  int[] c;

  char P[];
  int m;

  int[] Phi;
  int[] PLCP;
  int[] LCP;

  void countingSort(int k) {
    int i, sum, maxi = Math.max(300, n);
    for (i = 0; i < 100010; i++) c[i] = 0;
    for (i = 0; i < n; i++)
      c[i + k < n ? RA[i + k] : 0]++;
    for (i = sum = 0; i < maxi; i++) {
      int t = c[i]; c[i] = sum; sum += t;
    }
    for (i = 0; i < n; i++)
      tempSA[c[SA[i] + k < n ? RA[SA[i] + k] : 0]++] = SA[i];
    for (i = 0; i < n; i++)
      SA[i] = tempSA[i];
  }

  void constructSA() {
    int i, k, r;
    for (i = 0; i < n; i++) RA[i] = T[i];
    for (i = 0; i < n; i++) SA[i] = i;
    for (k = 1; k < n; k <<= 1) {
      countingSort(k);
      countingSort(0);
      tempRA[SA[0]] = r = 0;
      for (i = 1; i < n; i++)
        tempRA[SA[i]] = 
          (RA[SA[i]] == RA[SA[i-1]] && RA[SA[i]+k] == RA[SA[i-1]+k]) ? r : ++r;
      for (i = 0; i < n; i++)
        RA[i] = tempRA[i];
  } }

  void computeLCP() {
    int i, L;
    Phi[SA[0]] = -1;
    for (i = 1; i < n; i++)
      Phi[SA[i]] = SA[i-1];
    for (i = L = 0; i < n; i++) {
      if (Phi[i] == -1) { PLCP[i] = 0; continue; }
      while (i + L < T.length && Phi[i] + L < T.length && T[i + L] == T[Phi[i] + L]) L++;
      PLCP[i] = L;
      L = Math.max(L-1, 0);
    }
    for (i = 1; i < n; i++)
      LCP[i] = PLCP[SA[i]];
  }

  int strncmp(char[] a, int i, char[] b, int j, int n){
    for (int k=0; i+k < a.length && j+k < b.length; k++){
      if (a[i+k] != b[j+k]) return a[i+k] - b[j+k];
    }
    return 0;
  }

  int[] stringMatching() {
    int lo = 0, hi = n-1, mid = lo;
    while (lo < hi) {
      mid = (lo + hi) / 2;
      int res = strncmp(T, SA[mid], P, 0, m);
      if (res >= 0) hi = mid;
      else          lo = mid + 1;
    }
    if (strncmp(T,SA[lo], P,0, m) != 0) return new int[]{-1, -1};
    int[] ans = new int[]{ lo, 0} ;

    lo = 0; hi = n - 1; mid = lo;
    while (lo < hi) {
      mid = (lo + hi) / 2;
      int res = strncmp(T, SA[mid], P,0, m);
      if (res > 0) hi = mid;
      else         lo = mid + 1;
    }
    if (strncmp(T, SA[hi], P,0, m) != 0) hi--;
    ans[1] = hi;
    return ans;
  }

  void LRS() {
    int i, idx = 0, maxLCP = 0;

    for (i = 1; i < n; i++)
      if (LCP[i] > maxLCP) {
        maxLCP = LCP[i];
        idx = i;
      }
  }

  int owner(int idx) { return (idx < n-m-1) ? 1 : 2; }

  int getBorder() {
    int n = T.length;
    int res = -1;
    for(int i = 0; i < n; i += 1) {
      if(SA[i] == 0) {
        res = i;
        break;
      }
    }
    int i = res;
    while(i > 0 && n - SA[i - 1] != LCP[i]) {
        i -= 1;
    }
    if(n - SA[i - 1] == LCP[i]) {
        return LCP[i];
    }
    return -1;
  }

  void run(String s) {
    int MAX_N = 100010;
    c = new int[MAX_N];
    RA = new int[MAX_N];
    tempRA = new int[MAX_N];
    SA = new Integer[MAX_N];
    tempSA = new Integer[MAX_N];
    Phi = new int[MAX_N];
    PLCP = new int[MAX_N];
    LCP = new int[MAX_N];

    T = s.toCharArray();
    n = T.length;

    constructSA();
    computeLCP();

    int i = getBorder();

    if(i == -1) {
        System.out.println(0);
    } else {
        System.out.println(i);
    }

    /*for(int i = 0; i < T.length; i += 1) {
        System.out.println(SA[i] + " " + LCP[i]);
    }*/

  }

  public static void main(String[] args){
    
    Scanner scan = new Scanner(System.in);
    new Main().run(scan.nextLine());
  }
}