Find the first substring that contains no duplicates in the English string for efficient implementation (Modified Version)

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.