<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:georss="http://www.georss.org/georss" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:media="http://search.yahoo.com/mrss/"
		>
<channel>
	<title>Comments on: String Search: Rabin-Karp</title>
	<atom:link href="http://programmingpraxis.com/2009/09/01/string-search-rabin-karp/feed/" rel="self" type="application/rss+xml" />
	<link>http://programmingpraxis.com/2009/09/01/string-search-rabin-karp/</link>
	<description>A collection of etudes, updated weekly, for the education and enjoyment of the savvy programmer</description>
	<lastBuildDate>Sat, 11 Feb 2012 09:48:16 +0000</lastBuildDate>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.com/</generator>
	<item>
		<title>By: Vikas Tandi</title>
		<link>http://programmingpraxis.com/2009/09/01/string-search-rabin-karp/#comment-3007</link>
		<dc:creator><![CDATA[Vikas Tandi]]></dc:creator>
		<pubDate>Mon, 09 May 2011 08:39:31 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1234#comment-3007</guid>
		<description><![CDATA[my implementation in C
[sourcecode lang=&quot;cpp&quot;]
#include &lt;string.h&gt;

static long double rolling_hash(long double hashval, long double val, char s, char e);
static long double hash(char *s, int m);

int string_index_4(char *s, int slen, char *p, int plen)
{
	int i;
	long double hsub, hs, pow;

	/* sanity check */
	if(!s &#124;&#124; !p &#124;&#124; plen &lt;= 0)
		return -1;

	for(i = 1, pow = 1; i &lt;= (plen-1); i++)
		pow = pow * 256;

	/* calculate hash for sub-string and string */
	hs = hash(s, plen);
	hsub = hash(p, plen);

	/* search sub-string */
	for(i = 0; i &lt;= (slen - plen); i++)
	{
		if(hs == hsub)
			if(strncmp(&amp;s[i], p, plen) == 0)
				return i+1;

		hs = rolling_hash(hs, pow, s[i], s[i+plen]);
	}

	return -1;
}

static long double hash(char *s, int m)
{
	int i;
	long double hashval;
	for(hashval = 0.0, i = 0; s[i] != &#039;&#092;&#048;&#039; &amp;&amp; i &lt; m; i++)
		hashval = 256 * hashval + s[i];

	return hashval;
}

static long double rolling_hash(long double hashval, long double val, char s, char e)
{
	hashval = hashval - s * val;
	hashval = hashval * 256 + e;
	return hashval;
}
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>my implementation in C</p>
<pre class="brush: cpp;">
#include &lt;string.h&gt;

static long double rolling_hash(long double hashval, long double val, char s, char e);
static long double hash(char *s, int m);

int string_index_4(char *s, int slen, char *p, int plen)
{
	int i;
	long double hsub, hs, pow;

	/* sanity check */
	if(!s || !p || plen &lt;= 0)
		return -1;

	for(i = 1, pow = 1; i &lt;= (plen-1); i++)
		pow = pow * 256;

	/* calculate hash for sub-string and string */
	hs = hash(s, plen);
	hsub = hash(p, plen);

	/* search sub-string */
	for(i = 0; i &lt;= (slen - plen); i++)
	{
		if(hs == hsub)
			if(strncmp(&amp;s[i], p, plen) == 0)
				return i+1;

		hs = rolling_hash(hs, pow, s[i], s[i+plen]);
	}

	return -1;
}

static long double hash(char *s, int m)
{
	int i;
	long double hashval;
	for(hashval = 0.0, i = 0; s[i] != '&#092;&#048;' &amp;&amp; i &lt; m; i++)
		hashval = 256 * hashval + s[i];

	return hashval;
}

static long double rolling_hash(long double hashval, long double val, char s, char e)
{
	hashval = hashval - s * val;
	hashval = hashval * 256 + e;
	return hashval;
}
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Posts about Programming from google blogs as of September 1, 2009 &#171; tryfly.com</title>
		<link>http://programmingpraxis.com/2009/09/01/string-search-rabin-karp/#comment-562</link>
		<dc:creator><![CDATA[Posts about Programming from google blogs as of September 1, 2009 &#171; tryfly.com]]></dc:creator>
		<pubDate>Wed, 02 Sep 2009 00:05:08 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1234#comment-562</guid>
		<description><![CDATA[[...]  [...]]]></description>
		<content:encoded><![CDATA[<p>[...]  [...]</p>
]]></content:encoded>
	</item>
	<item>
		<title>By: Remco Niemeijer</title>
		<link>http://programmingpraxis.com/2009/09/01/string-search-rabin-karp/#comment-561</link>
		<dc:creator><![CDATA[Remco Niemeijer]]></dc:creator>
		<pubDate>Tue, 01 Sep 2009 18:09:10 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1234#comment-561</guid>
		<description><![CDATA[My Haskell solution (see http://bonsaicode.wordpress.com/2009/09/01/programming-praxis-string-search-rabin-karp/ for a version with comments):

[sourcecode lang=&#039;css&#039;]
import Data.Char
import Data.List

asciis :: String -&gt; [Integer]
asciis = map (fromIntegral . ord)

hash :: Num a =&gt; [a] -&gt; a
hash = sum . zipWith (*) (iterate (* 256) 1) . reverse

hashes :: Int -&gt; String -&gt; [Integer]
hashes p xs = scanl (\s (f,t) -&gt; 256*s - 256^p*f + t) (hash $ take p ascs) .
              zip ascs $ drop p ascs where ascs = asciis xs

rabinKarp :: String -&gt; Maybe Int -&gt; String -&gt; Maybe Int
rabinKarp p s = lookup (hash $ asciis p) . drop (maybe 0 id s) .
                flip zip [0..] . hashes (length p)
[/sourcecode]]]></description>
		<content:encoded><![CDATA[<p>My Haskell solution (see <a href="http://bonsaicode.wordpress.com/2009/09/01/programming-praxis-string-search-rabin-karp/" rel="nofollow">http://bonsaicode.wordpress.com/2009/09/01/programming-praxis-string-search-rabin-karp/</a> for a version with comments):</p>
<pre class="brush: css;">
import Data.Char
import Data.List

asciis :: String -&gt; [Integer]
asciis = map (fromIntegral . ord)

hash :: Num a =&gt; [a] -&gt; a
hash = sum . zipWith (*) (iterate (* 256) 1) . reverse

hashes :: Int -&gt; String -&gt; [Integer]
hashes p xs = scanl (\s (f,t) -&gt; 256*s - 256^p*f + t) (hash $ take p ascs) .
              zip ascs $ drop p ascs where ascs = asciis xs

rabinKarp :: String -&gt; Maybe Int -&gt; String -&gt; Maybe Int
rabinKarp p s = lookup (hash $ asciis p) . drop (maybe 0 id s) .
                flip zip [0..] . hashes (length p)
</pre>
]]></content:encoded>
	</item>
	<item>
		<title>By: Programming Praxis &#8211; String Search: Rabin-Karp &#171; Bonsai Code</title>
		<link>http://programmingpraxis.com/2009/09/01/string-search-rabin-karp/#comment-560</link>
		<dc:creator><![CDATA[Programming Praxis &#8211; String Search: Rabin-Karp &#171; Bonsai Code]]></dc:creator>
		<pubDate>Tue, 01 Sep 2009 18:08:52 +0000</pubDate>
		<guid isPermaLink="false">http://programmingpraxis.com/?p=1234#comment-560</guid>
		<description><![CDATA[[...] Praxis &#8211; String Search:&#160;Rabin-Karp By Remco Niemeijer  Today’s Programming Praxis problem marks the end of the string search series. In this final entry, we have [...]]]></description>
		<content:encoded><![CDATA[<p>[...] Praxis &#8211; String Search:&nbsp;Rabin-Karp By Remco Niemeijer  Today’s Programming Praxis problem marks the end of the string search series. In this final entry, we have [...]</p>
]]></content:encoded>
	</item>
</channel>
</rss>

