To find the substring with the most consecutive occurrences in a string

Source: Internet
Author: User

The general idea is:

First, the given string is deposited into the vector at each reduction of one head character, and then the number of consecutive occurrences of a substring starting with a is traversed in vector[0]. The way of comparison for that Vector[0] is a comparison of a character starting with a and a vector[1] if there is no further comparison with a substring beginning with AB. In turn.

The suffix array for inserting str in vector vectors:
/* suffix array such as: ABABABC, bababc,ababc,babc,abc,bc,c*/
#include <iostream>
#include <vector>
#include <string>
using namespace Std;
Pair<int, string> Fun (const string &str)
{
vector<string>substrs;
int maxcount = 1, count = 1;
String substr;
int I, Len = Str.length ();
for (i = 0; i < len; i++)
{
Substrs.push_back (Str.substr (i, len-i));//The substrings in the STR string are inserted into vector vectors in a way that decreases the head one at a time, string substr (int pos = 0,int n = NPOs) const;//returns the N-character string at the beginning of a POS
cout << substrs[i] << Endl;
}
for (i = 0; i < len; i++)//start with the first substring until all the substrings have been traversed
{
for (int j = i + 1; j < Len/2+1; J + +)//start with the next substring and look for successive substrings
{
Count = 1;
if (substrs[i].substr (0, j-i) = = Substrs[j].substr (0, j-i))//In the subject, look for a substring beginning with a, followed by a substring beginning with B, and a substring beginning with C
{
++count;
for (int k = j + (j-i); k < len; k + = J-i)
{
if (substrs[i].substr (0, j-i) = = Substrs[k].substr (0, J-i))
++count;//If there is a contiguous substring, the next or several characters in the next substring of the vector and where the same substring appears now
Else
Break
}
if (Count > Maxcount)//maxcount records the number of occurrences of the largest contiguous substring in all traversal
{
Maxcount = count;
substr = substrs[i].substr (0, j-i);
}
}
}
}
Return Make_pair (Maxcount, substr);//maxcout and found substring into pair<> return


}
int main ()
{
Pair<int, string> result;
String str = "ABCBCBCBCA";
result = Fun (str);
cout << result.first << "<< result.second << Endl;
System ("pause");
return 0;
}


Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

To find the substring with the most consecutive occurrences in a string

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.