|
| 1 | +program LongestPalindromicSubstring; |
| 2 | + |
| 3 | +{$mode objfpc}{$H+} |
| 4 | + |
| 5 | +uses |
| 6 | + SysUtils; |
| 7 | + |
| 8 | +procedure ShowUsage; |
| 9 | +begin |
| 10 | + Writeln('Usage: please provide a string that contains at least one palindrome'); |
| 11 | + Halt(1); |
| 12 | +end; |
| 13 | + |
| 14 | +function PreprocessString(const s: string): string; |
| 15 | +var |
| 16 | + builder: TStringBuilder; |
| 17 | + ch: char; |
| 18 | +begin |
| 19 | + builder := TStringBuilder.Create; |
| 20 | + builder.Append('#'); |
| 21 | + for ch in s do |
| 22 | + begin |
| 23 | + builder.Append(ch); |
| 24 | + builder.Append('#'); |
| 25 | + end; |
| 26 | + Result := builder.ToString; |
| 27 | +end; |
| 28 | + |
| 29 | +function LongestPalindromicSubstring(const s: string): string; |
| 30 | +var |
| 31 | + Processed: string; |
| 32 | + PalindromeRadii: array of integer; |
| 33 | + Center, RightBoundary, i, Mirror, LeftExpand, RightExpand, ProcessedLength: integer; |
| 34 | + MaxPalindromeLength, MaxCenterIndex, OriginalStartIndex: integer; |
| 35 | + |
| 36 | + function Min(A, B: integer): integer; inline; |
| 37 | + begin |
| 38 | + Result := B; |
| 39 | + if A < B then |
| 40 | + Result := A; |
| 41 | + end; |
| 42 | + |
| 43 | +begin |
| 44 | + if s = '' then |
| 45 | + Exit(''); |
| 46 | + |
| 47 | + Processed := PreprocessString(s); |
| 48 | + ProcessedLength := Length(Processed); |
| 49 | + SetLength(PalindromeRadii, ProcessedLength); |
| 50 | + |
| 51 | + Center := 0; |
| 52 | + RightBoundary := 0; |
| 53 | + MaxPalindromeLength := 0; |
| 54 | + MaxCenterIndex := 0; |
| 55 | + |
| 56 | + for i := 0 to ProcessedLength - 1 do |
| 57 | + begin |
| 58 | + Mirror := 2 * Center - i; |
| 59 | + if i < RightBoundary then |
| 60 | + PalindromeRadii[i] := Min(RightBoundary - i, PalindromeRadii[Mirror]); |
| 61 | + |
| 62 | + LeftExpand := i + (1 + PalindromeRadii[i]); |
| 63 | + RightExpand := i - (1 + PalindromeRadii[i]); |
| 64 | + |
| 65 | + while (LeftExpand < ProcessedLength) and (RightExpand >= 0) and |
| 66 | + (Processed[LeftExpand + 1] = Processed[RightExpand + 1]) do |
| 67 | + begin |
| 68 | + Inc(PalindromeRadii[i]); |
| 69 | + Inc(LeftExpand); |
| 70 | + Dec(RightExpand); |
| 71 | + end; |
| 72 | + |
| 73 | + if i + PalindromeRadii[i] > RightBoundary then |
| 74 | + begin |
| 75 | + Center := i; |
| 76 | + RightBoundary := i + PalindromeRadii[i]; |
| 77 | + end; |
| 78 | + |
| 79 | + if PalindromeRadii[i] > MaxPalindromeLength then |
| 80 | + begin |
| 81 | + MaxPalindromeLength := PalindromeRadii[i]; |
| 82 | + MaxCenterIndex := i; |
| 83 | + end; |
| 84 | + end; |
| 85 | + |
| 86 | + OriginalStartIndex := ((MaxCenterIndex - MaxPalindromeLength) div 2) + 1; |
| 87 | + Result := Copy(s, OriginalStartIndex, MaxPalindromeLength); |
| 88 | +end; |
| 89 | + |
| 90 | +var |
| 91 | + Input: string; |
| 92 | + LongestPalindrome: string; |
| 93 | +begin |
| 94 | + if ParamCount <> 1 then |
| 95 | + ShowUsage; |
| 96 | + |
| 97 | + Input := ParamStr(1); |
| 98 | + |
| 99 | + if Trim(Input) = '' then |
| 100 | + ShowUsage; |
| 101 | + |
| 102 | + LongestPalindrome := LongestPalindromicSubstring(Input); |
| 103 | + |
| 104 | + if LongestPalindrome.Length = 1 then |
| 105 | + ShowUsage; |
| 106 | + |
| 107 | + Writeln(LongestPalindrome); |
| 108 | +end. |
0 commit comments