Problem: longest_palindrome_substring

Your task is to implement Manacher's algorithm for the Longest Palindromic Substring and verify its correctness in Dafny. The algorithm identifies the substring of maximal length in input $s$ that reads the same forwards and backwards. The operation proceeds as follows: (1) Transformation: Construct a modified string $T$ by inserting a unique separator (e.g., '#') between every character of $s$ (and at the outer boundaries) to uniformly handle both odd and even length palindromes. (2) Initialization: Maintain an array $P$ where $P[i]$ represents the radius of the palindrome centered at $T[i]$. Initialize a center variable $C$ and a right boundary variable $R$ to $0$. (3) Iteration: Iterate through each index $i$ of $T$. If $i < R$, initialize $P[i]$ using the properties of symmetry around $C$ via the mirror index $i' = 2C - i$, specifically setting $P[i] = \min(R - i, P[i'])$. Otherwise, initialize $P[i]$ to $0$. (4) Expansion: Attempt to expand the palindrome centered at $i$ by checking if characters at $T[i - 1 - P[i]]$ and $T[i + 1 + P[i]]$ are equal. Increment $P[i]$ repeatedly while they match. (5) Boundary Update: If the newly expanded palindrome at $i$ extends beyond $R$ (i.e., $i + P[i] > R$), update the center $C$ to $i$ and the boundary $R$ to $i + P[i]$. (6) Result Extraction: Find the maximum value in $P$ and map its center and radius back to the corresponding start index and length in the original string $s$. Specifically, you must prove the following: (1) Functional Correctness: The returned tuple $(start, len)$ defines a valid subrange of $s$, the substring defined by this range satisfies the formal is_palindrome definition (symmetric characters), and for any other valid palindromic substring in $s$, its length is $\le len$. (2) Loop Invariants: You must demonstrate that for every processed center $k < i$, $P[k]$ stores the correct maximal palindrome radius for that center in $T$, and that the current variables $C$ and $R$ consistently mark the rightmost ending palindrome found so far (meaning the palindrome centered at $C$ extends exactly to $R$).

// Following is the block for necessary definitions
// <preamble>
// Definition of a Palindrome:
// A sequence is a palindrome if for every index i, the character at i
// is identical to the character at the symmetric position from the end.
predicate is_palindrome(s: seq<int>) {
    // We trigger on s[i] so the solver knows to use this rule 
    // whenever it sees an access to s at index i.
    forall i :: 0 <= i < |s| ==> s[i] == s[|s| - 1 - i]
}

// Helper to check validity of a subrange within a larger sequence.
predicate is_valid_subrange(s: seq<int>, start: int, len: int) {
    && 0 <= start
    && 0 <= len
    && start + len <= |s|
}
// </preamble>

// Following is the block for potential helper specifications
// <helpers>

// </helpers>

// Following is the block for proofs of lemmas
// <proofs>

// </proofs>

// Following is the block for the main specification
// <spec>
// The main verification target.
// Returns a tuple (start_index, length) representing the longest palindrome.
method longest_palindromic_substring(s: seq<int>) returns (res: (int, int))
    requires
        // CONSTRAINT: Hard limit of 1,000,000. 
        // Even with Manacher's expansion (2*N + 1), the required size 
        // is ~2,000,001, which fits easily in u32/usize.
        |s| <= 1000000
    ensures
        // Type Constraint: Results must be non-negative (simulating usize)
        res.0 >= 0 && res.1 >= 0
    ensures
        // 1. The result defines a valid substring of the input.
        is_valid_subrange(s, res.0, res.1)
    ensures
        // 2. The substring found is indeed a palindrome.
        is_palindrome(s[res.0 .. res.0 + res.1])
    ensures
        // 3. Maximality: There exists no other valid palindromic substring 
        // with a length strictly greater than the result found.
        forall i, len :: 
            is_valid_subrange(s, i, len) && is_palindrome(s[i .. i + len])
            ==> len <= res.1
// </spec>
// <code>
{
    // Implementation and verify of Manacher's algorithm goes here.
}
// </code>