The code written for practice at work. Turning it out.
Today, a friend pointed out that the program running result was incorrect. After trying it, there was indeed a problem. On the way to work, he reorganized his ideas and modified the code. The overall idea remained unchanged. He added several lines of code, process the final part of the string. Verify the result.
By the way, how can I cancel the system notification email when my post is commented?
Char * GetSubStr (const char * str)
{
Int hash [256]; // the position where each character appears in the hash record
Int I;
For (I = 0; I <256; I ++)
Hash [I] =-1;
Int CurrentStart = 0, MaxStart = 0, MaxEnd = 0, MaxLength = 0, CurrentLength = 0, strLen = strlen (str );
For (I = 0; I <strLen; I ++)
{
If (CurrentStart> hash [str [I]) // if there are no duplicates
{
Hash [str [I] = I;
} Www.2cto.com
Else
{
CurrentLength = I-CurrentStart; // current length
If (CurrentLength> MaxEnd-MaxStart) // if the current length is the longest
{
MaxEnd = I;
MaxStart = CurrentStart;
}
CurrentStart = hash [str [I] + 1; // update the current longest start point
Hash [str [I] = I; // position of the updated character
}
}
// Added code
If (strLen-CurrentStart> CurrentLength)
{
MaxEnd = strLen;
MaxStart = CurrentStart;
}
//
MaxLength = MaxEnd-MaxStart;
Char * reStr = new char [MaxLength + 1];
ReStr [MaxLength] = 0;
Memcpy (reStr, str + MaxStart, MaxLength );
Return reStr;
}
Author: ifeng