Example
"ABmadsefadd" has a maximum length of 7
"AVOAid" has a maximum length of 3
Ideas
Space change time Hashtable, starting position is set to beg. Initializes the global maximum value of 0. Opens a character array, initially labeled 0.
When accessing an array
- If the character's hash value in Hashtable corresponds to 1, the distance from the current position to beg is calculated, and the Beg is assigned beg+1. If it is greater than the global maximum, the global maximum value is replaced
- If the character has a hash value of 0 for the Hashtable, set 1
Reference Code
#include <iostream>using namespacestd;intGetmaxlen (Const string&s) { intBeg =0; intspan =0; intMaxspan =0; inthashtable[ -]; for(inti =0; I < -; ++i) hashtable[i]=0; intLENS =s.size (); for(inti =0; I < lens; ++i) {intindex =S[i]; if(Hashtable[index] = =1) {span= i-Beg; if(Span >maxspan) Maxspan=span; Beg++; } Else{Hashtable[s[i]]=1; } } returnMaxspan;}intMain () {Const stringA ="Abmadsefadd"; Const stringA1 ="Avoaid"; cout<< Getmaxlen (a) <<Endl; cout<< Getmaxlen (A1) <<Endl;}
Results
7 3
Algorithm--the longest non-coincident substring length in a string