<?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[Good-Suffix Table (Boyer-Moore) von C =&amp;gt; C++]]></title><description><![CDATA[<p>Hallo!<br />
Ich muss den Boyer-Moore Algorithmus aus dem Wikipedia (<a href="http://de.wikipedia.org/wiki/Boyer-Moore-Algorithmus" rel="nofollow">http://de.wikipedia.org/wiki/Boyer-Moore-Algorithmus</a>) in C++ implementieren und soll mit strings arbeiten und auf die gotos verzichten.<br />
Habe das mal versucht und leider stimmt bei mir irgendetwas nicht. Meiner Meinung nach müsste der Code funktionieren, habe wohl irgendwo nen Logikfehler eingebaut...</p>
<p>Weiß einer von euch ne Antwort?</p>
<p><strong>C:</strong></p>
<pre><code class="language-cpp">next = (int*)malloc((plen+1) * sizeof(int));

for (j = 0; j &lt;= plen; j++) {
   for (i = plen - 1; i &gt;= 1; i--) {
     for (k = 1; k &lt;= j; k++) {
       if (i - k &lt; 0) goto matched;
       if (p[plen - k] != p[i - k]) goto nexttry;
     }
   goto matched;
   nexttry: ;
   }
   matched: next[j] = plen - i;
 }
</code></pre>
<p>Werte:</p>
<pre><code>next[0] = 1
next[1] = 4
next[2] = 4
next[3] = 6
next[4] = 6
next[5] = 6
next[6] = 6
next[7] = 6
</code></pre>
<p><strong>Meine Lösung:</strong></p>
<pre><code class="language-cpp">cout &lt;&lt; endl &lt;&lt; &quot;GoodRule Table:&quot; &lt;&lt; endl;
	next = new int((patternLength + 1) * sizeof(int));

	for (j = 0; j &lt;= patternLength; j++) {
		i = patternLength - 1;
		bool matched = false;

		while (i &gt;= 1 &amp;&amp; matched == false) {
			k = 1;
			bool nexttry = false;

			while (k &lt;= j &amp;&amp; matched == false &amp;&amp; nexttry == false) {
				if (i - k &lt; 0)
					matched = true;

				if (pattern[patternLength - k] != pattern[i - k])
					nexttry = true;

				k++;
			}

			if (nexttry == false)
				matched = true;

			i--;
		}
		next[j] = patternLength - i + 1;
		cout &lt;&lt; &quot;next[&quot; &lt;&lt; j &lt;&lt; &quot;] = &quot; &lt;&lt; next[j] &lt;&lt; endl;
	}
</code></pre>
<p><strong>Werte:</strong></p>
<pre><code>next[0] = 3
next[1] = 6
next[2] = 6
next[3] = 8
next[4] = 8
next[5] = 8
next[6] = 8
next[7] = 8
</code></pre>
<p>Wie ihr seht, ist meine Lösung alles andere als elegant, was könnt ich verbessern?</p>
<p>Wäre sehr dankbar!</p>
<p>lg,<br />
Lemieux</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/208946/good-suffix-table-boyer-moore-von-c-gt-c</link><generator>RSS for Node</generator><lastBuildDate>Mon, 12 Oct 2026 08:19:38 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/208946.rss" rel="self" type="application/rss+xml"/><pubDate>Mon, 24 Mar 2008 22:47:33 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Good-Suffix Table (Boyer-Moore) von C =&amp;gt; C++ on Mon, 24 Mar 2008 22:48:30 GMT]]></title><description><![CDATA[<p>Hallo!<br />
Ich muss den Boyer-Moore Algorithmus aus dem Wikipedia (<a href="http://de.wikipedia.org/wiki/Boyer-Moore-Algorithmus" rel="nofollow">http://de.wikipedia.org/wiki/Boyer-Moore-Algorithmus</a>) in C++ implementieren und soll mit strings arbeiten und auf die gotos verzichten.<br />
Habe das mal versucht und leider stimmt bei mir irgendetwas nicht. Meiner Meinung nach müsste der Code funktionieren, habe wohl irgendwo nen Logikfehler eingebaut...</p>
<p>Weiß einer von euch ne Antwort?</p>
<p><strong>C:</strong></p>
<pre><code class="language-cpp">next = (int*)malloc((plen+1) * sizeof(int));

for (j = 0; j &lt;= plen; j++) {
   for (i = plen - 1; i &gt;= 1; i--) {
     for (k = 1; k &lt;= j; k++) {
       if (i - k &lt; 0) goto matched;
       if (p[plen - k] != p[i - k]) goto nexttry;
     }
   goto matched;
   nexttry: ;
   }
   matched: next[j] = plen - i;
 }
</code></pre>
<p>Werte:</p>
<pre><code>next[0] = 1
next[1] = 4
next[2] = 4
next[3] = 6
next[4] = 6
next[5] = 6
next[6] = 6
next[7] = 6
</code></pre>
<p><strong>Meine Lösung:</strong></p>
<pre><code class="language-cpp">cout &lt;&lt; endl &lt;&lt; &quot;GoodRule Table:&quot; &lt;&lt; endl;
	next = new int((patternLength + 1) * sizeof(int));

	for (j = 0; j &lt;= patternLength; j++) {
		i = patternLength - 1;
		bool matched = false;

		while (i &gt;= 1 &amp;&amp; matched == false) {
			k = 1;
			bool nexttry = false;

			while (k &lt;= j &amp;&amp; matched == false &amp;&amp; nexttry == false) {
				if (i - k &lt; 0)
					matched = true;

				if (pattern[patternLength - k] != pattern[i - k])
					nexttry = true;

				k++;
			}

			if (nexttry == false)
				matched = true;

			i--;
		}
		next[j] = patternLength - i + 1;
		cout &lt;&lt; &quot;next[&quot; &lt;&lt; j &lt;&lt; &quot;] = &quot; &lt;&lt; next[j] &lt;&lt; endl;
	}
</code></pre>
<p><strong>Werte:</strong></p>
<pre><code>next[0] = 3
next[1] = 6
next[2] = 6
next[3] = 8
next[4] = 8
next[5] = 8
next[6] = 8
next[7] = 8
</code></pre>
<p>Wie ihr seht, ist meine Lösung alles andere als elegant, was könnt ich verbessern?</p>
<p>Wäre sehr dankbar!</p>
<p>lg,<br />
Lemieux</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1479810</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1479810</guid><dc:creator><![CDATA[Lemieux]]></dc:creator><pubDate>Mon, 24 Mar 2008 22:48:30 GMT</pubDate></item><item><title><![CDATA[Reply to Good-Suffix Table (Boyer-Moore) von C =&amp;gt; C++ on Tue, 25 Mar 2008 00:01:45 GMT]]></title><description><![CDATA[<p>Hi Lemieux,</p>
<p>mir sind so ein paar Sachen an deinem Code aufgefallen:</p>
<pre><code class="language-cpp">cout &lt;&lt; endl &lt;&lt; &quot;GoodRule Table:&quot; &lt;&lt; endl;
    next = new int((patternLength + 1) /* * sizeof(int)*/); // mehr Speicher als nötig

    for (j = 0; j &lt;= patternLength; j++) {
        i = patternLength - 1;
        bool matched = false;

        while (i &gt;= 1 &amp;&amp; matched == false) {
            k = 1;
            bool nexttry = false;

            while (k &lt;= j &amp;&amp; matched == false &amp;&amp; nexttry == false) {
                if (i - k &lt; 0)
                    matched = true;

                /*else*/ if (pattern[patternLength - k] != pattern[i - k])
                    nexttry = true;

                /* else */k++; // Diese Zeile wird auch ausgeführt, wenn matched oder nexttry gesetzt werden (im Gegensatz zu Original)
            }

            if (nexttry == false)
                matched = true;
            /* if(!matched)*/
                i--; // analog zu oben
        }
        next[j] = patternLength - i + 1;
        cout &lt;&lt; &quot;next[&quot; &lt;&lt; j &lt;&lt; &quot;] = &quot; &lt;&lt; next[j] &lt;&lt; endl;
    }
</code></pre>
<p>Ich bin mir nicht sicher, ob das Ergebnis jetzt richtig ist, aber es sollte sich<br />
(mindestens) um eins nach unten verschieben.</p>
<p>Gruß,<br />
CSpille</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1479834</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1479834</guid><dc:creator><![CDATA[[[global:guest]]]]></dc:creator><pubDate>Tue, 25 Mar 2008 00:01:45 GMT</pubDate></item><item><title><![CDATA[Reply to Good-Suffix Table (Boyer-Moore) von C =&amp;gt; C++ on Tue, 25 Mar 2008 00:09:04 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">next = new int((patternLength + 1); // -&gt; * sizeof(int)) musst du hier weglassen!
for (j = 0; j &lt; patternLength; j++) {
    bool matched = false;
    for (i = patternLength - 1; i &gt; 1 &amp;&amp; matched == false; i--) {
        bool nexttry = false;
        for (k = 1; k &lt;= j &amp;&amp; matched == false &amp;&amp; nexttry == false; k++) {
            if (i - k &lt; 0) matched = true;
            else if (pattern[patternLength - k] != pattern[i - k]) nexttry = true;
        }
    }
    next[j] = patternLength - i + 1;
    cout &lt;&lt; &quot;next[&quot; &lt;&lt; j &lt;&lt; &quot;] = &quot; &lt;&lt; next[j] &lt;&lt; endl;
}
</code></pre>
<p>der code ist ungetestet und hat keinen Stil, trotzdem denke ich, dass es jetzt funktioniert...</p>
<p>MfG DrakoXP</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1479837</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1479837</guid><dc:creator><![CDATA[DrakoXP]]></dc:creator><pubDate>Tue, 25 Mar 2008 00:09:04 GMT</pubDate></item><item><title><![CDATA[Reply to Good-Suffix Table (Boyer-Moore) von C =&amp;gt; C++ on Tue, 25 Mar 2008 00:54:50 GMT]]></title><description><![CDATA[<p>CSpille schrieb:</p>
<blockquote>
<p>Hi Lemieux,</p>
<p>mir sind so ein paar Sachen an deinem Code aufgefallen:</p>
<pre><code class="language-cpp">cout &lt;&lt; endl &lt;&lt; &quot;GoodRule Table:&quot; &lt;&lt; endl;
    next = new int((patternLength + 1) /* * sizeof(int)*/); // mehr Speicher als nötig
    
    for (j = 0; j &lt;= patternLength; j++) {
        i = patternLength - 1;
        bool matched = false;
        
        while (i &gt;= 1 &amp;&amp; matched == false) {
            k = 1;
            bool nexttry = false;
            
            while (k &lt;= j &amp;&amp; matched == false &amp;&amp; nexttry == false) {
                if (i - k &lt; 0)
                    matched = true;
                
                /*else*/ if (pattern[patternLength - k] != pattern[i - k])
                    nexttry = true;
                
                /* else */k++; // Diese Zeile wird auch ausgeführt, wenn matched oder nexttry gesetzt werden (im Gegensatz zu Original)
            }
            
            if (nexttry == false)
                matched = true;
            /* if(!matched)*/
                i--; // analog zu oben
        }
        next[j] = patternLength - i + 1;
        cout &lt;&lt; &quot;next[&quot; &lt;&lt; j &lt;&lt; &quot;] = &quot; &lt;&lt; next[j] &lt;&lt; endl;
    }
</code></pre>
<p>Ich bin mir nicht sicher, ob das Ergebnis jetzt richtig ist, aber es sollte sich<br />
(mindestens) um eins nach unten verschieben.</p>
<p>Gruß,<br />
CSpille</p>
</blockquote>
<p>Funktioniert! Danke!</p>
<p>Danke auch an DrakoXP, hat aber nicht funktioniert <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1479844</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1479844</guid><dc:creator><![CDATA[Lemieux]]></dc:creator><pubDate>Tue, 25 Mar 2008 00:54:50 GMT</pubDate></item></channel></rss>