Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // with hash functions from http://pastebin.com/nb51hyvd you could do substring matching like this: (pseudocode)
- // features:
- // ** no extra data storage, in fact needle can be freed immediately (if you trust the hash)
- // ** only one loop through haystack, with each loop only consisting of 2 small hash operations
- int InStr(String haystack, String needle) {
- int needleHash = Hash(needle);
- int hash = Hash(SubString(haystack, 0, needle.length));
- if (hash == needleHash)
- return 0;
- for(int i = 0, j = needle.length; j < haystack.length; i++, j++)
- {
- hash = Hash_Remove(hash, haystack[i]); // remove the first
- hash = Hash_Add(hash, haystack[j]); // add the next
- if (hash == needleHash)
- return i + 1;
- }
- return -1;
- }
Advertisement
Add Comment
Please, Sign In to add comment