Prerequisites:
1. When using sort, you must specify using namespace STD; or directly type STD: Sort () with # include <algorithm>
2. qort is an upgraded version of qsort. If you can use sort as much as possible, it is easy to use. Unlike qsort, you have to write CMP functions by yourself,
You can use the library function as long as it is specified. There are only two parameters (for common usage), header pointer and tail pointer;
3. By default, sort is sorted in ascending order. To sort it in descending order, you can use your own CMP function.
Bool compare (int A, int B ){
Return A> B; // in descending order. If it is changed to return a <B, it is in ascending order.
}
Sort (* a, * B, CMP );
4. Pay special attention to the return value of double in the Compare function,
Obviously, CMP returns an integer, so it avoids the loss of digits returned by double.
Int CMP (const void * a, const void * B ){
Return * (double *) A> * (double *) B? 1:-1;
}
Analysis: It is required to output its position and its first character. For example, if the string is yyabcdabjcabceg, the output result should be ABC and 3.
1. The above strings can be decomposed into arrays with suffixes.
Substrs [0] = yyabcdabjcabceg;
Substrs [1] = yabcdabjcabceg;
Substrs [2] = abcdabjcabceg;
Substrs [3] = bcdabjcabceg;
Substrs [4] = cdabjcabceg;
Substrs [5] = dabjcabceg;
Substrs [6] = abjcabceg;
Substrs [7] = bjcabceg;
Substrs [8] = jcabceg;
Substrs [9] = cabceg;
Substrs [10] = abceg;
Substrs [11] = bceg;
Substrs [12] = CEG;
Substrs [13] = EG;
Substrs [14] = g;
2. Sort these strings alphabetically,
3. Then compare the precursor of the adjacent string and find the longest match.
# Include <iostream> # include <string> # include <vector> # include <algorithm> using namespace STD; pair <int, string> fun (const string & Str) {vector <string> substrs; int maxcount = 1; unsigned int COUNT = 0; string substr; int I, Len = Str. length (); for (I = 0; I <Len; I ++) // obtain all suffix substrings {// basic_string substr (size_type Pos = 0, size_type n = NPOs) const; // a string consisting of n characters starting from the POS. Substrs. push_back (Str. substr (I, len-I);} // sort (substrs. begin (), substrs. end (); // compare two adjacent string public sequences string str1; string str2; vector <string >:: iterator ITER; For (iter = substrs. begin (); iter! = Substrs. end ()-1;) {COUNT = 0; str1 = * ITER ++; str2 = * ITER; while (count <str1.length () & count <str2.length () & (str1.at (count) = str2.at (count) Count ++; If (count> maxcount) {maxcount = count; substr = str1.substr (0, maxcount) ;}} substrs. clear (); Return make_pair (maxcount, substr) ;}int main () {string STR; pair <int, string> RS; while (CIN> Str) {rs = fun (STR); cout <Rs. second <':' <Rs. first <Endl;} return 0 ;}
The following is another solution:
The time complexity is higher than that described above, but the space complexity is O (1). Time for space.
Train of Thought: For all substrings with all suffixes of the source string, starting from the oldest string of each suffix,
Search for matched substrings in the source string from the forward and backward directions, respectively,
If the two search locations are inconsistent, the string with the longest repetition length exists and the position of the forward query is returned.
E.g. String = "abcedfghiabckl". When a substring "ABC" is used to match forward and backward directions in the source string,
The positions found are pos1 = 0 and pos2 = 9.
# Include <iostream> # include <string> using namespace STD; int main () {string STR, TEP; CIN> STR; cout <"length =" <Str. length () <Endl; For (INT I = Str. length ()-1; I> = 1; I --) {for (Int J = 0; j <Str. length (); j ++) {// if there is no constraint for this sentence, the result is incorrect. // The length of the TEP obtained each time is I, that is, the string can be obtained from large to small. If (J + I <= Str. length () {size_t T = 0; size_t num = 0; TEP = Str. substr (J, I); // string from large to small. I is the length of the intercepted string. The longest string is intercepted first, and the second longer T = Str. find (TEP); // search for num = STR in positive order. rfind (TEP); // reverse lookup // If the locations of the two lookup operations are different, duplicate strings exist. If (T! = Num) // output when the condition is met, because it is truncated from the longest string. {Cout <TEP <"" <t <Endl; return 0 ;}}} return 0 ;}