Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <pre><code>class Palindrome { private int center; private int radius; public Palindrome(int center, int radius) { if (radius &lt; 0 || radius &gt; center) throw new Exception("Invalid palindrome."); this.center = center; this.radius = radius; } public int GetMirror(int index) { int i = 2 * center - index; if (i &lt; 0) return 0; return i; } public int GetCenter() { return center; } public int GetLength() { return 2 * radius; } public int GetRight() { return center + radius; } public int GetLeft() { return center - radius; } public void Expand() { ++radius; } public bool LargerThan(Palindrome other) { if (other == null) return false; return (radius &gt; other.radius); } } private static string GetFormatted(string original) { if (original == null) return null; else if (original.Length == 0) return ""; StringBuilder builder = new StringBuilder("#"); foreach (char c in original) { builder.Append(c); builder.Append('#'); } return builder.ToString(); } private static string GetUnFormatted(string formatted) { if (formatted == null) return null; else if (formatted.Length == 0) return ""; StringBuilder builder = new StringBuilder(); foreach (char c in formatted) { if (c != '#') builder.Append(c); } return builder.ToString(); } public static string FindLargestPalindrome(string str) { string formatted = GetFormatted(str); if (formatted == null || formatted.Length == 0) return formatted; int[] radius = new int[formatted.Length]; try { Palindrome current = new Palindrome(0, 0); for (int i = 0; i &lt; formatted.Length; ++i) { radius[i] = (current.GetRight() &gt; i) ? Math.Min(current.GetRight() - i, radius[current.GetMirror(i)]) : 0; current = new Palindrome(i, radius[i]); while (current.GetLeft() - 1 &gt;= 0 &amp;&amp; current.GetRight() + 1 &lt; formatted.Length &amp;&amp; formatted[current.GetLeft() - 1] == formatted[current.GetRight() + 1]) { current.Expand(); ++radius[i]; } } Palindrome largest = new Palindrome(0, 0); for (int i = 0; i &lt; radius.Length; ++i) { current = new Palindrome(i, radius[i]); if (current.LargerThan(largest)) largest = current; } return GetUnFormatted(formatted.Substring(largest.GetLeft(), largest.GetLength())); } catch (Exception ex) { Console.WriteLine(ex); } return null; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload