The string is shown in the form of a suffix tree as follows:
A b c A b c a B c D E. substr[0]
bcabcabcde....substr[1]
cabcabcde.......substr[2]
abcabcde..........substr[3]
bcabcde.............substr[4]
c A b c D e ..... ..... substr[5]
A b c d e ..... ...... substr[6]
b c d e ..... ..... ..... substr[7]
C d e ..... ..... substr[8..---"
D e ... ...... substr[9..........
E ... ..... substr[10..---------...
It can be observed that if there is a continuous string, the Substr[0].substr (i,j-i) = = Substr[j].substr (0,j-i) is satisfied, for example in the previous example
Substr[0].substr (0,3-0) = = Substr[3].substr (0,3-0)
In another way, we don't need to generate suffix groups , but the idea is the same.
Code:
The code in STR.SUBSTR (Pos2,offset) is actually equivalent to the suffix Group of substr[pos2].substr (0,offset)
Writing a string into a suffix group is actually the equivalent of standing in a different position looking back at the array, so there is no need to add additional storage space to generate the suffix group.
Program:
Pair<int, String>fun (const string &str)
{
vector<string> substrs;
int maxcount = 1;
NT count = 1;
String substr;
int i;
int len = Str.length ();
for (i = 0; i < len; i++)
Substrs.push_back (Str.substr (i, Len));//generate suffix substring
for (i = 0; i < len ; i++)
{
for (int j = i + 1; j < Len; j + +)
{
count = 1;
if (substrs[i].substr (0, j-i) = = Substrs[j].substr (0, j-i))//offet is j-i
{
count++;
for (int k = j + j-i; k < Len; k++)//In OFFET units, to find, discontinuous exit
{
if (substrs[i].substr (0, j-i) = = Substrs [K].substr (0, j-i))
count++;
else//exits
break without a succession;
if (Count > Maxcount)
{
Maxcount = count;
substr = substrs[i].substr (0, j-i);
}
}
}
Return Make_pair (Maxcount, substr);
}
int main ()
{
String str= "ABCABCABCCC";
Pair<int, string> Res;
res = fun (str);
cout << res.first << "<< Res.second;
Cin.get ();
return 0;
}
The most contiguous substring in a string