<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[laengstes palindrom algo]]></title><description><![CDATA[<p>hi,<br />
bitte um feedback zum folgenden algo. im moment ist die laufzeit des algos O(n^2), speicher: O(1)<br />
was kann die laufzeit noch schneller machen?</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;string&gt;
using namespace std;

void get_longest_pal(const string &amp;s, int middle_index, int l, int r, string &amp;longest_pal) {
	bool found = false;
	string pal;

	if(middle_index != -1) {
		pal = s[middle_index];
	}

	while(l &gt;= 0 &amp;&amp; r &lt; s.size()) {
		if(s[l] == s[r]) {
			pal = pal + s[r];
			pal = s[l] + pal;
			found = true;
			l--;
			r++;
		}
		else {
			break;
		}
	}

	if(found &amp;&amp; pal.size() &gt; longest_pal.size()) {
		longest_pal = pal;
	}
}

string find_longest_palindrome(string str) {
	string longest_pal;

	for(int i = 0; i &lt; str.size(); i++) {
		// pair element in the middle
		if(i &lt; str.size() - 1 &amp;&amp; str[i] == str[i+1]) {
			get_longest_pal(str, -1, i, i+1, longest_pal);
		}	
		// single element in the middle
		if(i &lt; str.size() - 2 &amp;&amp; str[i] == str[i+2]) {
			get_longest_pal(str, i+1, i, i+2, longest_pal);
		}
	}

	return longest_pal;
}

int main() {
	// your code goes here

	cout &lt;&lt; find_longest_palindrome(&quot;aaabbaaaccdeqjncsdddmmmkkkmmmddd&quot;) &lt;&lt; endl;

	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/326505/laengstes-palindrom-algo</link><generator>RSS for Node</generator><lastBuildDate>Mon, 25 May 2026 14:56:23 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/326505.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 21 Jun 2014 15:18:31 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to laengstes palindrom algo on Sat, 21 Jun 2014 15:18:31 GMT]]></title><description><![CDATA[<p>hi,<br />
bitte um feedback zum folgenden algo. im moment ist die laufzeit des algos O(n^2), speicher: O(1)<br />
was kann die laufzeit noch schneller machen?</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;string&gt;
using namespace std;

void get_longest_pal(const string &amp;s, int middle_index, int l, int r, string &amp;longest_pal) {
	bool found = false;
	string pal;

	if(middle_index != -1) {
		pal = s[middle_index];
	}

	while(l &gt;= 0 &amp;&amp; r &lt; s.size()) {
		if(s[l] == s[r]) {
			pal = pal + s[r];
			pal = s[l] + pal;
			found = true;
			l--;
			r++;
		}
		else {
			break;
		}
	}

	if(found &amp;&amp; pal.size() &gt; longest_pal.size()) {
		longest_pal = pal;
	}
}

string find_longest_palindrome(string str) {
	string longest_pal;

	for(int i = 0; i &lt; str.size(); i++) {
		// pair element in the middle
		if(i &lt; str.size() - 1 &amp;&amp; str[i] == str[i+1]) {
			get_longest_pal(str, -1, i, i+1, longest_pal);
		}	
		// single element in the middle
		if(i &lt; str.size() - 2 &amp;&amp; str[i] == str[i+2]) {
			get_longest_pal(str, i+1, i, i+2, longest_pal);
		}
	}

	return longest_pal;
}

int main() {
	// your code goes here

	cout &lt;&lt; find_longest_palindrome(&quot;aaabbaaaccdeqjncsdddmmmkkkmmmddd&quot;) &lt;&lt; endl;

	return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2404933</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2404933</guid><dc:creator><![CDATA[thomasmueller]]></dc:creator><pubDate>Sat, 21 Jun 2014 15:18:31 GMT</pubDate></item><item><title><![CDATA[Reply to laengstes palindrom algo on Sat, 21 Jun 2014 15:39:21 GMT]]></title><description><![CDATA[<p>ja kann man noch schneller machen<br />
<a href="http://en.wikipedia.org/wiki/Longest_palindromic_substring" rel="nofollow">http://en.wikipedia.org/wiki/Longest_palindromic_substring</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2404934</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2404934</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Sat, 21 Jun 2014 15:39:21 GMT</pubDate></item><item><title><![CDATA[Reply to laengstes palindrom algo on Sat, 21 Jun 2014 15:40:17 GMT]]></title><description><![CDATA[<p>ist mein algo wenigstens gut lesbar?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2404935</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2404935</guid><dc:creator><![CDATA[thomasmueller]]></dc:creator><pubDate>Sat, 21 Jun 2014 15:40:17 GMT</pubDate></item><item><title><![CDATA[Reply to laengstes palindrom algo on Sat, 21 Jun 2014 15:45:40 GMT]]></title><description><![CDATA[<p>nö</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2404936</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2404936</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Sat, 21 Jun 2014 15:45:40 GMT</pubDate></item><item><title><![CDATA[Reply to laengstes palindrom algo on Sun, 22 Jun 2014 08:21:55 GMT]]></title><description><![CDATA[<pre><code>string find_longest_palindrome(string str) {
</code></pre>
<p>warum nicht</p>
<pre><code>string find_longest_palindrome(const string&amp; str) {
</code></pre>
<p>wird ja weder verändert noch sonst was...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2404990</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2404990</guid><dc:creator><![CDATA[ConchitaWurst]]></dc:creator><pubDate>Sun, 22 Jun 2014 08:21:55 GMT</pubDate></item><item><title><![CDATA[Reply to laengstes palindrom algo on Sun, 22 Jun 2014 20:08:51 GMT]]></title><description><![CDATA[<p>Wobei... hauptsächlich stört:<br />
* dass du recht unsprechende Variablennamen verwendest<br />
* dass <code>get_longest_pal</code> zwei dinge macht die ich persönlich auftrennen würde:</p>
<ol>
<li>das längste palindrom suchen das bei l, middle_indel, r anängt</li>
<li>gucken ob es länger als das bisher gefundene längste palindrom ist und ggf. <code>longest_pal</code> updaten</li>
</ol>
<p>Dass du <code>pal</code> während der Suche zusammenbaust verwirrt auch eher.</p>
<p>Nach ein paar kleinen Umbauten sieht dein Code so aus, was ich persönlich besser zu lesen finde:</p>
<pre><code class="language-cpp">string get_longest_palindrome_at(string const&amp; s, int left_index, int right_index)
{
    assert(left_index + 1 == right_index || left_index + 2 == right_index);

    // check if there is a palindrome with that center position (mid-point between left_index and right_index) at all
    if (s[left_index] != s[right_index])
        return &quot;&quot;;

    // extend bounds as much as possible
    while (left_index &gt; 0 &amp;&amp; (right_index + 1) &lt; s.size())
    {
        if (s[left_index - 1] == s[right_index + 1])
        {
            left_index--;
            right_index++;
        }
        else
            break;
    }

    return string(s.begin() + left_index, s.begin() + right_index + 1);
}

void update_longest_palindrome(string const&amp; s, int left_index, int right_index, string&amp; longest_palindrome)
{
    // update longest_palindrome by comparing with the longest palindrome with the specified
    // center position (mid-point between left_index and right_index)
    if (left_index &gt;= 0 &amp;&amp; right_index &lt; s.size())
    {
        string const candidate = get_longest_palindrome_at(s, left_index, right_index);
        if (candidate.size() &gt; longest_palindrome.size())
            longest_palindrome = candidate;
    }
}

string find_longest_palindrome(string const&amp; s)
{
    string longest_palindrome;

    for (int i = 0; i &lt; s.size(); i++)
    {
        // pair element in the middle
        update_longest_palindrome(s, i, i + 1, longest_palindrome);

        // single element in the middle
        update_longest_palindrome(s, i, i + 2, longest_palindrome);
    }

    return longest_palindrome;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2405108</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2405108</guid><dc:creator><![CDATA[hustbaer]]></dc:creator><pubDate>Sun, 22 Jun 2014 20:08:51 GMT</pubDate></item></channel></rss>