Editing Longest palindromic substring

Jump to: navigation, search

Warning: You are not logged in. Your IP address will be publicly visible if you make any edits. If you log in or create an account, your edits will be attributed to your username, along with other benefits.

The edit can be undone. Please check the comparison below to verify that this is what you want to do, and then save the changes below to finish undoing the edit.
Latest revision Your text
Line 44: Line 44:
 
''Lemma'' (Second observation): If <math>s</math> is the longest palindrome centered at the current position, and <math>t</math> is the longest palindromic proper suffix of <math>s</math>, then all positions between the current position and the centre of <math>t</math> may safely be denoted uninteresting.
 
''Lemma'' (Second observation): If <math>s</math> is the longest palindrome centered at the current position, and <math>t</math> is the longest palindromic proper suffix of <math>s</math>, then all positions between the current position and the centre of <math>t</math> may safely be denoted uninteresting.
  
''Proof'': Consider some position <math>i</math> between the centres of <math>s</math> and <math>t</math>. Suppose there exists a palindromic substring, <math>u</math>, of length greater than or equal to that of <math>s</math>, centered at <math>i</math>. Certainly the last character of <math>u</math> lies to the right of the last character of <math>s</math>, since <math>u</math> is centered further to the right and is longer. So we repeatedly remove one character from each end of <math>u</math> until <math>u</math> and <math>s</math> end at the same position. Since <math>u</math> is now shorter than <math>s</math> (since its centre is to the right but both <math>s</math> and <math>u</math> extend equally far on the right), it is a palindromic proper suffix of <math>s</math>. Since the centre of <math>u</math> is to the left of the centre of <math>t</math>, <math>u</math> is longer than <math>t</math>, a contradiction as <math>t</math> is maximal by hypothesis.
+
''Proof'': Consider some position <math>i</math> between the centres of <math>s</math> and <math>t</math>. Suppose there exists a palindromic substring, <math>u</math>, of length greater than or equal to that of <math>s</math>, centered at <math>i</math>. Certainly the last character of <math>u</math> lies to the right of the last character of <math>s</math>, since <math>u</math> is centered further to the right and is longer. So we repeatedly remove one character from each end of <math>u</math> until <math>u</math> and <math>s</math> end at the same position. Since <math>u</math> is now shorter than <math>s</math> (since its centre is to the right but both <math>s</math> and <math>t</math> extend equally far on the right), it is a palindromic proper suffix of <math>s</math>. Since the centre of <math>u</math> is to the left of the centre of <math>t</math>, <math>u</math> is longer than <math>t</math>, a contradiction as <math>t</math> is maximal by hypothesis.
  
 
To find the longest palindromic proper suffix, we make yet another observation: the longest palindromic proper suffix and the longest palindromic proper prefix are reflections of each other about the current centre. So we scan backward from the current position. For each position <math>i</math> to the left of the current position, we know the longest palindromic substring <math>u</math> centered there. If the first character of <math>u</math> is to the right of the first character of <math>s</math>, then the longest palindromic substring centered at the position obtained by reflecting <math>u</math> about the current position is identical to <math>s</math>. (If it were possible to extend it, it would also be possible to extend <math>s</math>.) This means we can fill in the corresponding position in the longest palindromic array immediately, and also that there is no palindromic prefix of <math>s</math> centered at <math>i</math> as <math>u</math>, and hence no corresponding palindromic suffix at the position obtained by reflecting <math>i</math> across the current centre. Otherwise, on the other hand, we know there ''is'' a palindromic (proper) prefix centered at <math>i</math>, which means there is a palindromic (proper) suffix centered at the position obtained by reflecting <math>i</math> across the current centre. Furthermore, since we are scanning backward from the current centre, the ''first'' such <math>i</math> we encounter is the centre of the ''longest'' palindromic proper prefix, so we have also found the ''longest'' palindromic proper suffix.
 
To find the longest palindromic proper suffix, we make yet another observation: the longest palindromic proper suffix and the longest palindromic proper prefix are reflections of each other about the current centre. So we scan backward from the current position. For each position <math>i</math> to the left of the current position, we know the longest palindromic substring <math>u</math> centered there. If the first character of <math>u</math> is to the right of the first character of <math>s</math>, then the longest palindromic substring centered at the position obtained by reflecting <math>u</math> about the current position is identical to <math>s</math>. (If it were possible to extend it, it would also be possible to extend <math>s</math>.) This means we can fill in the corresponding position in the longest palindromic array immediately, and also that there is no palindromic prefix of <math>s</math> centered at <math>i</math> as <math>u</math>, and hence no corresponding palindromic suffix at the position obtained by reflecting <math>i</math> across the current centre. Otherwise, on the other hand, we know there ''is'' a palindromic (proper) prefix centered at <math>i</math>, which means there is a palindromic (proper) suffix centered at the position obtained by reflecting <math>i</math> across the current centre. Furthermore, since we are scanning backward from the current centre, the ''first'' such <math>i</math> we encounter is the centre of the ''longest'' palindromic proper prefix, so we have also found the ''longest'' palindromic proper suffix.

Please note that all contributions to PEGWiki are considered to be released under the Attribution 3.0 Unported (see PEGWiki:Copyrights for details). If you do not want your writing to be edited mercilessly and redistributed at will, then do not submit it here.
You are also promising us that you wrote this yourself, or copied it from a public domain or similar free resource. Do not submit copyrighted work without permission!

Cancel | Editing help (opens in new window)

Templates used on this page: