Permalink
Browse files

merge.

  • Loading branch information...
2 parents de1b8bd + fbe13d3 commit 0e2ab957257e96d09e92f5291c040f0e5bb83e19 @Xorcerer committed Dec 17, 2013
Showing with 24 additions and 11 deletions.
  1. +24 −11 longest-common-substring/acapla.cpp
View
35 longest-common-substring/acapla.cpp
@@ -9,30 +9,44 @@ using namespace std;
inline int FindLongestSubstring(const string& x, const string& y, string* outLongestSubstring)
{
- auto sizeX = x.size();
- auto sizeY = y.size();
+ int sizeX = x.size();
+ int sizeY = y.size();
+ const string* px = &x;
+ const string* py = &y;
+ int sslength = sizeX;
+ int lslength = sizeY;
if(sizeX > sizeY)
{
- std::swap(const_cast<string&>(x), const_cast<string&>(y));
- std::swap(sizeX, sizeY);
+ swap(px, py);
+ swap(sslength, lslength);
}
+ const string& ss = *px;
+ const string& ls = *py;
+
int longest = 0;
- auto longestBegin = 0;
+ int longestBegin = 0;
- for(auto offset = 0; offset <= sizeY - sizeX; ++offset)
+ //ss:abcdefg
+ //ls: 123456789 this is negative offset = -6
+ //ss: abcdefg
+ //ls:123456789 this is positive offset = 4
+ for(int offset = -(sslength - 1); offset < lslength - longest; ++offset)
{
int currentLongest = 0;
int currentLength = 0;
- for(auto i = 0; i < sizeX; ++i)
+ const int startIndexSS = offset < 0 ? -offset : 0;
+ const int startIndexLS = offset < 0 ? 0 : offset;
+
+ for(int iss = startIndexSS, ils = startIndexLS; iss < sslength && ils < lslength; ++iss, ++ils)
{
- if(x[i] != y[offset + i])
+ if(ss[iss] != ls[ils])
{
if(currentLength > longest)
{
currentLongest = currentLength;
longest = currentLongest;
- longestBegin = offset + i - currentLength;
+ longestBegin = ils - currentLength;
}
currentLength = 0;
@@ -47,13 +61,12 @@ inline int FindLongestSubstring(const string& x, const string& y, string* outLon
{
currentLongest = max(currentLength, currentLongest);
longest = max(longest, currentLongest);
- longestBegin = offset;
}
}
if(longest != 0)
{
- *outLongestSubstring = y.substr(longestBegin, longest);
+ *outLongestSubstring = ls.substr(longestBegin, longest);
}
else
{

0 comments on commit 0e2ab95

Please sign in to comment.