Under construction

Still being built. Things may change

String Rank

Time limit0.5sMemory limit2048 MB

Summary
Given a string w of length up to 3 times 10^6, find the smallest t such that all suffixes of w have pairwise different sets of subsequences of length at most t.
Level

Hard8 of 10

Topics
String, Greedy, Implementation
Solved
No attempts yet

Problem

Let ww and uu be strings of lowercase English letters. A string uu is a subsequence of ww if there is a strictly increasing sequence of integers i1,⋯ ,iki_1, \cdots, i_k with ∣w∣=n|w| = n, ∣u∣=k|u| = k, and u[j]=w[ij]u[j] = w[i_j] for all j=1,…,kj = 1, \dots, k. Here, v[i]v[i] is the ii-th character of the string vv. Let w[i:]w[i:] denote the suffix w[i]⋯w[n]w[i] \cdots w[n]. If i>ni > n, then w[i:]w[i:] is the empty string, denoted λ\lambda.

Given a nonempty string ww and a positive integer kk, the kk-set of ww is the set Qk(w)Q_k(w) of subsequences of ww whose lengths are 0,1,⋯ ,k0, 1, \cdots, k. By this definition, the empty string λ\lambda is always in Qk(w)Q_k(w).

For example, when ww is aaba, Q3(aaba)={λ,a,b,ba,ab,aa,aba,aab,aaa}Q_3(\text{aaba}) = \{\lambda, \text{a}, \text{b}, \text{ba}, \text{ab}, \text{aa}, \text{aba}, \text{aab}, \text{aaa}\}.

The rank of ww is the minimum integer tt such that the tt-sets of all suffixes of ww are pairwise different. Formally, the rank is min⁡{t≥1∣Qt(w[i:])≠Qt(w[j:]) for all 1≤i<j≤n}\min\{t \ge 1 \mid Q_t(w[i:]) \ne Q_t(w[j:]) \text{ for all } 1 \le i < j \le n\}.

For instance, when ww is aaba, the 2-sets Q2(aba)Q_2(\text{aba}) and Q2(aaba)Q_2(\text{aaba}) are equal. For t=3t = 3, the suffix sets are Q3(λ)={λ}Q_3(\lambda) = \{\lambda\}, Q3(a)={λ,a}Q_3(\text{a}) = \{\lambda, \text{a}\}, Q3(ba)={λ,a,b,ba}Q_3(\text{ba}) = \{\lambda, \text{a}, \text{b}, \text{ba}\}, Q3(aba)={λ,a,b,ba,ab,aa,aba}Q_3(\text{aba}) = \{\lambda, \text{a}, \text{b}, \text{ba}, \text{ab}, \text{aa}, \text{aba}\}, and Q3(aaba)={λ,a,b,ba,ab,aa,aba,aab,aaa}Q_3(\text{aaba}) = \{\lambda, \text{a}, \text{b}, \text{ba}, \text{ab}, \text{aa}, \text{aba}, \text{aab}, \text{aaa}\}. All five are different, so the rank of aaba is 3.

Input

The input consists of a single nonempty string ww made only of lowercase English letters. The length of ww is at most 3×1063 \times 10^6.

Output

Print exactly one line containing a positive integer, the rank tt of ww.

Examples4

  1. Example 1

    Input
    aabbb
    
    Expected output
    3
    
  2. Example 2

    Input
    abacb
    
    Expected output
    2
    
  3. Example 3

    Input
    azadzzadaz
    
    Expected output
    4
    
  4. Example 4

    Input
    a
    
    Expected output
    1