Files
C-Plus-Plus/d3/d39/manacher__algorithm_8cpp.html
2022-01-02 02:55:05 +00:00

374 lines
36 KiB
HTML

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "https://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<meta http-equiv="X-UA-Compatible" content="IE=11"/>
<meta name="generator" content="Doxygen 1.9.2"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>Algorithms_in_C++: strings/manacher_algorithm.cpp File Reference</title>
<link href="../../tabs.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../jquery.js"></script>
<script type="text/javascript" src="../../dynsections.js"></script>
<link href="../../navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../resize.js"></script>
<script type="text/javascript" src="../../navtreedata.js"></script>
<script type="text/javascript" src="../../navtree.js"></script>
<link href="../../search/search.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="../../search/searchdata.js"></script>
<script type="text/javascript" src="../../search/search.js"></script>
<script type="text/x-mathjax-config">
MathJax.Hub.Config({
extensions: ["tex2jax.js", "TeX/AMSmath.js", "TeX/AMSsymbols.js"],
jax: ["input/TeX","output/HTML-CSS"],
});
</script>
<script type="text/javascript" async="async" src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.7/MathJax.js?config=TeX-MML-AM_CHTML/MathJax.js"></script>
<link href="../../doxygen.css" rel="stylesheet" type="text/css" />
</head>
<body>
<div id="top"><!-- do not remove this div, it is closed by doxygen! -->
<div id="titlearea">
<table cellspacing="0" cellpadding="0">
<tbody>
<tr style="height: 56px;">
<td id="projectalign" style="padding-left: 0.5em;">
<div id="projectname">Algorithms_in_C++<span id="projectnumber">&#160;1.0.0</span>
</div>
<div id="projectbrief">Set of algorithms implemented in C++.</div>
</td>
</tr>
</tbody>
</table>
</div>
<!-- end header part -->
<!-- Generated by Doxygen 1.9.2 -->
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
var searchBox = new SearchBox("searchBox", "../../search",'Search','.html');
/* @license-end */
</script>
<script type="text/javascript" src="../../menudata.js"></script>
<script type="text/javascript" src="../../menu.js"></script>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(function() {
initMenu('../../',true,false,'search.php','Search');
$(document).ready(function() { init_search(); });
});
/* @license-end */
</script>
<div id="main-nav"></div>
</div><!-- top -->
<div id="side-nav" class="ui-resizable side-nav-resizable">
<div id="nav-tree">
<div id="nav-tree-contents">
<div id="nav-sync" class="sync"></div>
</div>
</div>
<div id="splitbar" style="-moz-user-select:none;"
class="ui-resizable-handle">
</div>
</div>
<script type="text/javascript">
/* @license magnet:?xt=urn:btih:d3d9a9a6595521f9666a5e94cc830dab83b65699&amp;dn=expat.txt MIT */
$(document).ready(function(){initNavTree('d3/d39/manacher__algorithm_8cpp.html','../../'); initResizable(); });
/* @license-end */
</script>
<div id="doc-content">
<!-- window showing the filter options -->
<div id="MSearchSelectWindow"
onmouseover="return searchBox.OnSearchSelectShow()"
onmouseout="return searchBox.OnSearchSelectHide()"
onkeydown="return searchBox.OnSearchSelectKey(event)">
</div>
<!-- iframe showing the search results (closed by default) -->
<div id="MSearchResultsWindow">
<iframe src="javascript:void(0)" frameborder="0"
name="MSearchResults" id="MSearchResults">
</iframe>
</div>
<div class="header">
<div class="summary">
<a href="#namespaces">Namespaces</a> &#124;
<a href="#func-members">Functions</a> </div>
<div class="headertitle"><div class="title">manacher_algorithm.cpp File Reference</div></div>
</div><!--header-->
<div class="contents">
<p>Implementation of <a href="https://en.wikipedia.org/wiki/Longest_palindromic_substring" target="_blank">Manacher's Algorithm</a>
<a href="#details">More...</a></p>
<div class="textblock"><code>#include &lt;cassert&gt;</code><br />
<code>#include &lt;iostream&gt;</code><br />
<code>#include &lt;vector&gt;</code><br />
<code>#include &lt;cstring&gt;</code><br />
</div><div class="textblock"><div class="dynheader">
Include dependency graph for manacher_algorithm.cpp:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../dd/d05/manacher__algorithm_8cpp__incl.svg" width="342" height="112"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="namespaces" name="namespaces"></a>
Namespaces</h2></td></tr>
<tr class="memitem:df/dcb/namespacestrings"><td class="memItemLeft" align="right" valign="top">namespace &#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../df/dcb/namespacestrings.html">strings</a></td></tr>
<tr class="memdesc:df/dcb/namespacestrings"><td class="mdescLeft">&#160;</td><td class="mdescRight">Algorithms with strings. <br /></td></tr>
<tr class="separator:"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:d8/dc6/namespacemanacher"><td class="memItemLeft" align="right" valign="top">namespace &#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d8/dc6/namespacemanacher.html">manacher</a></td></tr>
<tr class="memdesc:d8/dc6/namespacemanacher"><td class="mdescLeft">&#160;</td><td class="mdescRight">Functions for <a href="https://en.wikipedia.org/wiki/Longest_palindromic_substring" target="_blank">Manacher's Algorithm</a> implementation. <br /></td></tr>
<tr class="separator:"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table><table class="memberdecls">
<tr class="heading"><td colspan="2"><h2 class="groupheader"><a id="func-members" name="func-members"></a>
Functions</h2></td></tr>
<tr class="memitem:a95e6a2976bb2f332898f373941d52098"><td class="memItemLeft" align="right" valign="top"><a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string.html">std::string</a>&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d3/d39/manacher__algorithm_8cpp.html#a95e6a2976bb2f332898f373941d52098">strings::manacher::manacher</a> (const <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string.html">std::string</a> &amp;prototype)</td></tr>
<tr class="memdesc:a95e6a2976bb2f332898f373941d52098"><td class="mdescLeft">&#160;</td><td class="mdescRight">A function that implements Manacher's algorithm. <a href="../../d3/d39/manacher__algorithm_8cpp.html#a95e6a2976bb2f332898f373941d52098">More...</a><br /></td></tr>
<tr class="separator:a95e6a2976bb2f332898f373941d52098"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:aa8dca7b867074164d5f45b0f3851269d"><td class="memItemLeft" align="right" valign="top">static void&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d3/d39/manacher__algorithm_8cpp.html#aa8dca7b867074164d5f45b0f3851269d">test</a> ()</td></tr>
<tr class="memdesc:aa8dca7b867074164d5f45b0f3851269d"><td class="mdescLeft">&#160;</td><td class="mdescRight">Self-test implementations. <a href="../../d3/d39/manacher__algorithm_8cpp.html#aa8dca7b867074164d5f45b0f3851269d">More...</a><br /></td></tr>
<tr class="separator:aa8dca7b867074164d5f45b0f3851269d"><td class="memSeparator" colspan="2">&#160;</td></tr>
<tr class="memitem:ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="memItemLeft" align="right" valign="top">int&#160;</td><td class="memItemRight" valign="bottom"><a class="el" href="../../d3/d39/manacher__algorithm_8cpp.html#ae66f6b31b5ad750f1fe042a706a4e3d4">main</a> ()</td></tr>
<tr class="memdesc:ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="mdescLeft">&#160;</td><td class="mdescRight">Main function. <a href="../../d3/d39/manacher__algorithm_8cpp.html#ae66f6b31b5ad750f1fe042a706a4e3d4">More...</a><br /></td></tr>
<tr class="separator:ae66f6b31b5ad750f1fe042a706a4e3d4"><td class="memSeparator" colspan="2">&#160;</td></tr>
</table>
<a name="details" id="details"></a><h2 class="groupheader">Detailed Description</h2>
<div class="textblock"><p >Implementation of <a href="https://en.wikipedia.org/wiki/Longest_palindromic_substring" target="_blank">Manacher's Algorithm</a> </p>
<p >Manacher's Algorithm is used to find the longest palindromic substring within a string in O(n) time. It exploits the property of a palindrome that its first half is symmetric to the last half, and thus if the first half is a palindrome, then last half is also a palindrome. </p><dl class="section author"><dt>Author</dt><dd><a href="https://github.com/riti2409" target="_blank">Riti Kumari</a> </dd></dl>
</div><h2 class="groupheader">Function Documentation</h2>
<a id="ae66f6b31b5ad750f1fe042a706a4e3d4" name="ae66f6b31b5ad750f1fe042a706a4e3d4"></a>
<h2 class="memtitle"><span class="permalink"><a href="#ae66f6b31b5ad750f1fe042a706a4e3d4">&#9670;&nbsp;</a></span>main()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname">int main </td>
<td>(</td>
<td class="paramtype">void&#160;</td>
<td class="paramname"></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>Main function. </p>
<dl class="section return"><dt>Returns</dt><dd>0 on exit </dd></dl>
<div class="fragment"><div class="line"><a id="l00171" name="l00171"></a><span class="lineno"> 171</span> {</div>
<div class="line"><a id="l00172" name="l00172"></a><span class="lineno"> 172</span> <a class="code hl_function" href="../../d3/d39/manacher__algorithm_8cpp.html#aa8dca7b867074164d5f45b0f3851269d">test</a>(); <span class="comment">// run self-test implementations</span></div>
<div class="line"><a id="l00173" name="l00173"></a><span class="lineno"> 173</span> <span class="keywordflow">return</span> 0;</div>
<div class="line"><a id="l00174" name="l00174"></a><span class="lineno"> 174</span>}</div>
<div class="ttc" id="amanacher__algorithm_8cpp_html_aa8dca7b867074164d5f45b0f3851269d"><div class="ttname"><a href="../../d3/d39/manacher__algorithm_8cpp.html#aa8dca7b867074164d5f45b0f3851269d">test</a></div><div class="ttdeci">static void test()</div><div class="ttdoc">Self-test implementations.</div><div class="ttdef"><b>Definition:</b> manacher_algorithm.cpp:150</div></div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d3/d39/manacher__algorithm_8cpp_ae66f6b31b5ad750f1fe042a706a4e3d4_cgraph.svg" width="274" height="38"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div>
</div>
<a id="a95e6a2976bb2f332898f373941d52098" name="a95e6a2976bb2f332898f373941d52098"></a>
<h2 class="memtitle"><span class="permalink"><a href="#a95e6a2976bb2f332898f373941d52098">&#9670;&nbsp;</a></span>manacher()</h2>
<div class="memitem">
<div class="memproto">
<table class="memname">
<tr>
<td class="memname"><a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string.html">std::string</a> strings::manacher::manacher </td>
<td>(</td>
<td class="paramtype">const <a class="elRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string.html">std::string</a> &amp;&#160;</td>
<td class="paramname"><em>prototype</em></td><td>)</td>
<td></td>
</tr>
</table>
</div><div class="memdoc">
<p>A function that implements Manacher's algorithm. </p>
<dl class="params"><dt>Parameters</dt><dd>
<table class="params">
<tr><td class="paramname">prototype</td><td>is the string where algorithm finds a palindromic substring. This string can contain any character except <code>@</code> <code>#</code> <code>&amp;</code> </td></tr>
</table>
</dd>
</dl>
<dl class="section return"><dt>Returns</dt><dd>the largest palindromic substring </dd></dl>
<div class="fragment"><div class="line"><a id="l00040" name="l00040"></a><span class="lineno"> 40</span> {</div>
<div class="line"><a id="l00041" name="l00041"></a><span class="lineno"> 41</span> <span class="keywordflow">if</span> (prototype.<a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string/size.html">size</a>() &gt; 0) {</div>
<div class="line"><a id="l00042" name="l00042"></a><span class="lineno"> 42</span> <span class="comment">// stuffing characters between the input string to handle cases with</span></div>
<div class="line"><a id="l00043" name="l00043"></a><span class="lineno"> 43</span> <span class="comment">// even length palindrome</span></div>
<div class="line"><a id="l00044" name="l00044"></a><span class="lineno"> 44</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string.html">std::string</a> stuffed_string = <span class="stringliteral">&quot;&quot;</span>;</div>
<div class="line"><a id="l00045" name="l00045"></a><span class="lineno"> 45</span> <span class="keywordflow">for</span> (<span class="keyword">auto</span> str : prototype) {</div>
<div class="line"><a id="l00046" name="l00046"></a><span class="lineno"> 46</span> stuffed_string += str;</div>
<div class="line"><a id="l00047" name="l00047"></a><span class="lineno"> 47</span> stuffed_string += <span class="stringliteral">&quot;#&quot;</span>;</div>
<div class="line"><a id="l00048" name="l00048"></a><span class="lineno"> 48</span> }</div>
<div class="line"><a id="l00049" name="l00049"></a><span class="lineno"> 49</span> stuffed_string = <span class="stringliteral">&quot;@#&quot;</span> + stuffed_string + <span class="stringliteral">&quot;&amp;&quot;</span>;</div>
<div class="line"><a id="l00050" name="l00050"></a><span class="lineno"> 50</span> </div>
<div class="line"><a id="l00051" name="l00051"></a><span class="lineno"> 51</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector&lt;uint64_t&gt;</a> palindrome_max_half_length(</div>
<div class="line"><a id="l00052" name="l00052"></a><span class="lineno"> 52</span> stuffed_string.<a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string/size.html">size</a>(),</div>
<div class="line"><a id="l00053" name="l00053"></a><span class="lineno"> 53</span> 0); <span class="comment">// this array will consist of largest possible half length of</span></div>
<div class="line"><a id="l00054" name="l00054"></a><span class="lineno"> 54</span> <span class="comment">// palindrome centered at index (say i with respect to the</span></div>
<div class="line"><a id="l00055" name="l00055"></a><span class="lineno"> 55</span> <span class="comment">// stuffed string). This value will be lower bound of half</span></div>
<div class="line"><a id="l00056" name="l00056"></a><span class="lineno"> 56</span> <span class="comment">// length since single character is a palindrome in itself.</span></div>
<div class="line"><a id="l00057" name="l00057"></a><span class="lineno"> 57</span> </div>
<div class="line"><a id="l00058" name="l00058"></a><span class="lineno"> 58</span> uint64_t bigger_center =</div>
<div class="line"><a id="l00059" name="l00059"></a><span class="lineno"> 59</span> 0; <span class="comment">// this is the index of the center of palindromic</span></div>
<div class="line"><a id="l00060" name="l00060"></a><span class="lineno"> 60</span> <span class="comment">// substring which would be considered as the larger</span></div>
<div class="line"><a id="l00061" name="l00061"></a><span class="lineno"> 61</span> <span class="comment">// palindrome, having symmetric halves</span></div>
<div class="line"><a id="l00062" name="l00062"></a><span class="lineno"> 62</span> </div>
<div class="line"><a id="l00063" name="l00063"></a><span class="lineno"> 63</span> uint64_t <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/manip/left.html">right</a> = 0; <span class="comment">// this is the maximum length of the palindrome</span></div>
<div class="line"><a id="l00064" name="l00064"></a><span class="lineno"> 64</span> <span class="comment">// from &#39;bigger_center&#39; to the rightmost end</span></div>
<div class="line"><a id="l00065" name="l00065"></a><span class="lineno"> 65</span> </div>
<div class="line"><a id="l00066" name="l00066"></a><span class="lineno"> 66</span> <span class="comment">// i is considered as center lying within one half of the palindrone</span></div>
<div class="line"><a id="l00067" name="l00067"></a><span class="lineno"> 67</span> <span class="comment">// which is centered at &#39;bigger_center&#39;</span></div>
<div class="line"><a id="l00068" name="l00068"></a><span class="lineno"> 68</span> <span class="keywordflow">for</span> (uint64_t i = 1; i &lt; stuffed_string.<a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string/size.html">size</a>() - 1; i++) {</div>
<div class="line"><a id="l00069" name="l00069"></a><span class="lineno"> 69</span> <span class="keywordflow">if</span> (i &lt; right) { <span class="comment">// when i is before right end, considering</span></div>
<div class="line"><a id="l00070" name="l00070"></a><span class="lineno"> 70</span> <span class="comment">// &#39;bigger_center&#39; as center of palindrome</span></div>
<div class="line"><a id="l00071" name="l00071"></a><span class="lineno"> 71</span> uint64_t opposite_to_i =</div>
<div class="line"><a id="l00072" name="l00072"></a><span class="lineno"> 72</span> 2 * bigger_center -</div>
<div class="line"><a id="l00073" name="l00073"></a><span class="lineno"> 73</span> i; <span class="comment">// this is the opposite end of string, if</span></div>
<div class="line"><a id="l00074" name="l00074"></a><span class="lineno"> 74</span> <span class="comment">// centered at center, and having one end as i</span></div>
<div class="line"><a id="l00075" name="l00075"></a><span class="lineno"> 75</span> </div>
<div class="line"><a id="l00076" name="l00076"></a><span class="lineno"> 76</span> <span class="comment">// finding the minimum possible half length among</span></div>
<div class="line"><a id="l00077" name="l00077"></a><span class="lineno"> 77</span> <span class="comment">// the palindrome on having center at opposite end,</span></div>
<div class="line"><a id="l00078" name="l00078"></a><span class="lineno"> 78</span> <span class="comment">// and the string between i and right end,</span></div>
<div class="line"><a id="l00079" name="l00079"></a><span class="lineno"> 79</span> <span class="comment">// considering &#39;bigger_center&#39; as center of palindrome</span></div>
<div class="line"><a id="l00080" name="l00080"></a><span class="lineno"> 80</span> palindrome_max_half_length[i] = <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/algorithm/min.html">std::min</a>(</div>
<div class="line"><a id="l00081" name="l00081"></a><span class="lineno"> 81</span> palindrome_max_half_length[opposite_to_i], right - i);</div>
<div class="line"><a id="l00082" name="l00082"></a><span class="lineno"> 82</span> }</div>
<div class="line"><a id="l00083" name="l00083"></a><span class="lineno"> 83</span> </div>
<div class="line"><a id="l00084" name="l00084"></a><span class="lineno"> 84</span> <span class="comment">// expanding the palindrome across the maximum stored length in the</span></div>
<div class="line"><a id="l00085" name="l00085"></a><span class="lineno"> 85</span> <span class="comment">// array, centered at i</span></div>
<div class="line"><a id="l00086" name="l00086"></a><span class="lineno"> 86</span> <span class="keywordflow">while</span> (stuffed_string[i + (palindrome_max_half_length[i] + 1)] ==</div>
<div class="line"><a id="l00087" name="l00087"></a><span class="lineno"> 87</span> stuffed_string[i - (palindrome_max_half_length[i] + 1)]) {</div>
<div class="line"><a id="l00088" name="l00088"></a><span class="lineno"> 88</span> palindrome_max_half_length[i]++;</div>
<div class="line"><a id="l00089" name="l00089"></a><span class="lineno"> 89</span> }</div>
<div class="line"><a id="l00090" name="l00090"></a><span class="lineno"> 90</span> </div>
<div class="line"><a id="l00091" name="l00091"></a><span class="lineno"> 91</span> <span class="comment">// if palindrome centered at i exceeds the rightmost end of</span></div>
<div class="line"><a id="l00092" name="l00092"></a><span class="lineno"> 92</span> <span class="comment">// palindrome centered at &#39;bigger_center&#39;, then i will be made the</span></div>
<div class="line"><a id="l00093" name="l00093"></a><span class="lineno"> 93</span> <span class="comment">// &#39;bigger_center&#39; and right value will also be updated with respect</span></div>
<div class="line"><a id="l00094" name="l00094"></a><span class="lineno"> 94</span> <span class="comment">// to center i</span></div>
<div class="line"><a id="l00095" name="l00095"></a><span class="lineno"> 95</span> <span class="keywordflow">if</span> (i + palindrome_max_half_length[i] &gt; right) {</div>
<div class="line"><a id="l00096" name="l00096"></a><span class="lineno"> 96</span> bigger_center = i;</div>
<div class="line"><a id="l00097" name="l00097"></a><span class="lineno"> 97</span> <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/manip/left.html">right</a> = i + palindrome_max_half_length[i];</div>
<div class="line"><a id="l00098" name="l00098"></a><span class="lineno"> 98</span> }</div>
<div class="line"><a id="l00099" name="l00099"></a><span class="lineno"> 99</span> }</div>
<div class="line"><a id="l00100" name="l00100"></a><span class="lineno"> 100</span> </div>
<div class="line"><a id="l00101" name="l00101"></a><span class="lineno"> 101</span> <span class="comment">// now extracting the first largest palindrome</span></div>
<div class="line"><a id="l00102" name="l00102"></a><span class="lineno"> 102</span> uint64_t half_length = 0; <span class="comment">// half length of the largest palindrome</span></div>
<div class="line"><a id="l00103" name="l00103"></a><span class="lineno"> 103</span> uint64_t center_index = 0; <span class="comment">// index of center of the largest palindrome</span></div>
<div class="line"><a id="l00104" name="l00104"></a><span class="lineno"> 104</span> </div>
<div class="line"><a id="l00105" name="l00105"></a><span class="lineno"> 105</span> <span class="keywordflow">for</span> (uint64_t i = 1; i &lt; stuffed_string.<a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string/size.html">size</a>() - 1; i++) {</div>
<div class="line"><a id="l00106" name="l00106"></a><span class="lineno"> 106</span> <span class="keywordflow">if</span> (palindrome_max_half_length[i] &gt; half_length) {</div>
<div class="line"><a id="l00107" name="l00107"></a><span class="lineno"> 107</span> half_length = palindrome_max_half_length[i];</div>
<div class="line"><a id="l00108" name="l00108"></a><span class="lineno"> 108</span> center_index = i;</div>
<div class="line"><a id="l00109" name="l00109"></a><span class="lineno"> 109</span> }</div>
<div class="line"><a id="l00110" name="l00110"></a><span class="lineno"> 110</span> }</div>
<div class="line"><a id="l00111" name="l00111"></a><span class="lineno"> 111</span> </div>
<div class="line"><a id="l00112" name="l00112"></a><span class="lineno"> 112</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/string/basic_string.html">std::string</a> palindromic_substring =</div>
<div class="line"><a id="l00113" name="l00113"></a><span class="lineno"> 113</span> <span class="stringliteral">&quot;&quot;</span>; <span class="comment">// contains the resulting largest palindrome</span></div>
<div class="line"><a id="l00114" name="l00114"></a><span class="lineno"> 114</span> </div>
<div class="line"><a id="l00115" name="l00115"></a><span class="lineno"> 115</span> <span class="keywordflow">if</span> (half_length &gt; 0) {</div>
<div class="line"><a id="l00116" name="l00116"></a><span class="lineno"> 116</span> <span class="comment">// extra information: when &#39;#&#39; is the center, then palindromic</span></div>
<div class="line"><a id="l00117" name="l00117"></a><span class="lineno"> 117</span> <span class="comment">// substring will have even length, else palindromic substring will</span></div>
<div class="line"><a id="l00118" name="l00118"></a><span class="lineno"> 118</span> <span class="comment">// have odd length</span></div>
<div class="line"><a id="l00119" name="l00119"></a><span class="lineno"> 119</span> </div>
<div class="line"><a id="l00120" name="l00120"></a><span class="lineno"> 120</span> uint64_t start =</div>
<div class="line"><a id="l00121" name="l00121"></a><span class="lineno"> 121</span> center_index - half_length +</div>
<div class="line"><a id="l00122" name="l00122"></a><span class="lineno"> 122</span> 1; <span class="comment">// index of first character of palindromic substring</span></div>
<div class="line"><a id="l00123" name="l00123"></a><span class="lineno"> 123</span> uint64_t <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/iterator/end.html">end</a> =</div>
<div class="line"><a id="l00124" name="l00124"></a><span class="lineno"> 124</span> center_index + half_length -</div>
<div class="line"><a id="l00125" name="l00125"></a><span class="lineno"> 125</span> 1; <span class="comment">// index of last character of palindromic substring</span></div>
<div class="line"><a id="l00126" name="l00126"></a><span class="lineno"> 126</span> <span class="keywordflow">for</span> (uint64_t index = start; index &lt;= <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/iterator/end.html">end</a>; index += 2) {</div>
<div class="line"><a id="l00127" name="l00127"></a><span class="lineno"> 127</span> palindromic_substring += stuffed_string[index];</div>
<div class="line"><a id="l00128" name="l00128"></a><span class="lineno"> 128</span> }</div>
<div class="line"><a id="l00129" name="l00129"></a><span class="lineno"> 129</span> } <span class="keywordflow">else</span> {</div>
<div class="line"><a id="l00130" name="l00130"></a><span class="lineno"> 130</span> <span class="comment">// if length = 0, then there does not exist any palindrome of length</span></div>
<div class="line"><a id="l00131" name="l00131"></a><span class="lineno"> 131</span> <span class="comment">// &gt; 1 so we can assign any character of length 1 from string as the</span></div>
<div class="line"><a id="l00132" name="l00132"></a><span class="lineno"> 132</span> <span class="comment">// palindromic substring</span></div>
<div class="line"><a id="l00133" name="l00133"></a><span class="lineno"> 133</span> palindromic_substring = prototype[0];</div>
<div class="line"><a id="l00134" name="l00134"></a><span class="lineno"> 134</span> }</div>
<div class="line"><a id="l00135" name="l00135"></a><span class="lineno"> 135</span> <span class="keywordflow">return</span> palindromic_substring;</div>
<div class="line"><a id="l00136" name="l00136"></a><span class="lineno"> 136</span> </div>
<div class="line"><a id="l00137" name="l00137"></a><span class="lineno"> 137</span> } <span class="keywordflow">else</span> {</div>
<div class="line"><a id="l00138" name="l00138"></a><span class="lineno"> 138</span> <span class="comment">// handling case when string is empty</span></div>
<div class="line"><a id="l00139" name="l00139"></a><span class="lineno"> 139</span> <span class="keywordflow">return</span> <span class="stringliteral">&quot;&quot;</span>;</div>
<div class="line"><a id="l00140" name="l00140"></a><span class="lineno"> 140</span> }</div>
<div class="line"><a id="l00141" name="l00141"></a><span class="lineno"> 141</span>}</div>
<div class="ttc" id="abasic_string_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/string/basic_string.html">std::string</a></div></div>
<div class="ttc" id="aend_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/iterator/end.html">std::end</a></div><div class="ttdeci">T end(T... args)</div></div>
<div class="ttc" id="aleft_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/io/manip/left.html">std::right</a></div><div class="ttdeci">T right(T... args)</div></div>
<div class="ttc" id="amin_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/algorithm/min.html">std::min</a></div><div class="ttdeci">T min(T... args)</div></div>
<div class="ttc" id="asize_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/string/basic_string/size.html">std::string::size</a></div><div class="ttdeci">T size(T... args)</div></div>
<div class="ttc" id="avector_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/container/vector.html">std::vector</a></div></div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d3/d39/manacher__algorithm_8cpp_a95e6a2976bb2f332898f373941d52098_cgraph.svg" width="306" height="88"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div>
</div>
<a id="aa8dca7b867074164d5f45b0f3851269d" name="aa8dca7b867074164d5f45b0f3851269d"></a>
<h2 class="memtitle"><span class="permalink"><a href="#aa8dca7b867074164d5f45b0f3851269d">&#9670;&nbsp;</a></span>test()</h2>
<div class="memitem">
<div class="memproto">
<table class="mlabels">
<tr>
<td class="mlabels-left">
<table class="memname">
<tr>
<td class="memname">static void test </td>
<td>(</td>
<td class="paramname"></td><td>)</td>
<td></td>
</tr>
</table>
</td>
<td class="mlabels-right">
<span class="mlabels"><span class="mlabel">static</span></span> </td>
</tr>
</table>
</div><div class="memdoc">
<p>Self-test implementations. </p>
<dl class="section return"><dt>Returns</dt><dd>void </dd></dl>
<div class="fragment"><div class="line"><a id="l00150" name="l00150"></a><span class="lineno"> 150</span> {</div>
<div class="line"><a id="l00151" name="l00151"></a><span class="lineno"> 151</span> assert(strings::manacher::manacher(<span class="stringliteral">&quot;&quot;</span>) == <span class="stringliteral">&quot;&quot;</span>);</div>
<div class="line"><a id="l00152" name="l00152"></a><span class="lineno"> 152</span> assert(strings::manacher::manacher(<span class="stringliteral">&quot;abababc&quot;</span>) == <span class="stringliteral">&quot;ababa&quot;</span>);</div>
<div class="line"><a id="l00153" name="l00153"></a><span class="lineno"> 153</span> assert(strings::manacher::manacher(<span class="stringliteral">&quot;cbaabd&quot;</span>) == <span class="stringliteral">&quot;baab&quot;</span>);</div>
<div class="line"><a id="l00154" name="l00154"></a><span class="lineno"> 154</span> assert(strings::manacher::manacher(<span class="stringliteral">&quot;DedzefDeD&quot;</span>) == <span class="stringliteral">&quot;DeD&quot;</span>);</div>
<div class="line"><a id="l00155" name="l00155"></a><span class="lineno"> 155</span> assert(strings::manacher::manacher(<span class="stringliteral">&quot;XZYYXXYZXX&quot;</span>) == <span class="stringliteral">&quot;YXXY&quot;</span>);</div>
<div class="line"><a id="l00156" name="l00156"></a><span class="lineno"> 156</span> assert(strings::manacher::manacher(<span class="stringliteral">&quot;1sm222m10abc&quot;</span>) == <span class="stringliteral">&quot;m222m&quot;</span>);</div>
<div class="line"><a id="l00157" name="l00157"></a><span class="lineno"> 157</span> assert(strings::manacher::manacher(<span class="stringliteral">&quot;798989591&quot;</span>) == <span class="stringliteral">&quot;98989&quot;</span>);</div>
<div class="line"><a id="l00158" name="l00158"></a><span class="lineno"> 158</span> assert(strings::manacher::manacher(<span class="stringliteral">&quot;xacdedcax&quot;</span>) == <span class="stringliteral">&quot;xacdedcax&quot;</span>);</div>
<div class="line"><a id="l00159" name="l00159"></a><span class="lineno"> 159</span> assert(strings::manacher::manacher(<span class="stringliteral">&quot;xaccax&quot;</span>) == <span class="stringliteral">&quot;xaccax&quot;</span>);</div>
<div class="line"><a id="l00160" name="l00160"></a><span class="lineno"> 160</span> assert(strings::manacher::manacher(<span class="stringliteral">&quot;a&quot;</span>) == <span class="stringliteral">&quot;a&quot;</span>);</div>
<div class="line"><a id="l00161" name="l00161"></a><span class="lineno"> 161</span> assert(strings::manacher::manacher(<span class="stringliteral">&quot;xy&quot;</span>) == <span class="stringliteral">&quot;x&quot;</span>);</div>
<div class="line"><a id="l00162" name="l00162"></a><span class="lineno"> 162</span> assert(strings::manacher::manacher(<span class="stringliteral">&quot;abced&quot;</span>) == <span class="stringliteral">&quot;a&quot;</span>);</div>
<div class="line"><a id="l00163" name="l00163"></a><span class="lineno"> 163</span> </div>
<div class="line"><a id="l00164" name="l00164"></a><span class="lineno"> 164</span> <a class="code hl_classRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a> &lt;&lt; <span class="stringliteral">&quot;All tests have passed!&quot;</span> &lt;&lt; <a class="code hl_functionRef" target="_blank" href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a>;</div>
<div class="line"><a id="l00165" name="l00165"></a><span class="lineno"> 165</span>}</div>
<div class="ttc" id="abasic_ostream_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/io/basic_ostream.html">std::cout</a></div></div>
<div class="ttc" id="aendl_html"><div class="ttname"><a href="http://en.cppreference.com/w/cpp/io/manip/endl.html">std::endl</a></div><div class="ttdeci">T endl(T... args)</div></div>
</div><!-- fragment --><div class="dynheader">
Here is the call graph for this function:</div>
<div class="dyncontent">
<div class="center"><iframe scrolling="no" frameborder="0" src="../../d3/d39/manacher__algorithm_8cpp_aa8dca7b867074164d5f45b0f3851269d_cgraph.svg" width="175" height="38"><p><b>This browser is not able to show SVG: try Firefox, Chrome, Safari, or Opera instead.</b></p></iframe>
</div>
</div>
</div>
</div>
</div><!-- contents -->
</div><!-- doc-content -->
<!-- start footer part -->
<div id="nav-path" class="navpath"><!-- id is needed for treeview function! -->
<ul>
<li class="navelem"><a class="el" href="../../dir_73a3cc5065b223eb41b02873c0e19f0e.html">strings</a></li><li class="navelem"><a class="el" href="../../d3/d39/manacher__algorithm_8cpp.html">manacher_algorithm.cpp</a></li>
<li class="footer">Generated by <a href="https://www.doxygen.org/index.html"><img class="footer" src="../../doxygen.svg" width="104" height="31" alt="doxygen"/></a> 1.9.2 </li>
</ul>
</div>
</body>
</html>