C ++ Example 7

Source: Internet
Author: User

# Include <iostream>
Using namespace STD;
// Reverse order string CHS to string RCHS.
// Parameter: CHS [] original string, RCHS [] Destination string, LEN Length
Inline void reverse (const char CHS [], char RCHS [], int Len ){
Int rindex = Len;
For (INT I = 0; I <Len; ++ I ){
RCHS [-- rindex] = CHS [I];
}
RCHS [Len] = '/0 ';
}

// Obtain the temporary string and place it in temp.
// Parameter: temp temporary string, CHS original string, offset, 0-(len-1), Len temp String Length
Inline void getchs (char temp [], const char CHS [], int offset, int Len ){
Int temp_index =-1;
For (INT I = 0; I <Len; ++ I ){
Temp [++ temp_index] = CHS [Offset + I];
}
Temp [Len] = '/0 ';
Cout <"the temp:" <temp <Endl;
}

// Verify whether the string temp exists in the string RCHS. If yes, true is returned. Otherwise, false is returned.
// Parameter: temp temporary string. RCHS are tested to contain temp strings, Len temp string length, and limit the number of characters tested by RCHS.
Inline bool compare (const char temp [], const char RCHS [], int Len, int limit ){
Int II = 0; // The character index tested by RCHS
Int offset = 0; // The offset in temp, Which is reset for each match
Bool first = false; // indicates whether the first character of temp is matched.
// If (strcmp (temp, "sabcd") = 0 ){
// Cout <"Len:" <strlen (temp) <Endl;
//}
While (II <Limit & offset <Len) {// The loop ends when the specified number is found or the specified temp has been matched in RCHS.
If (! First ){
If (temp [0] = RCHS [II]) {
First = true; // match the first character of temp
Offset = 1; // The offset is set to the second character.
}
} Else {
If (temp [offset ++]! = RCHS [II]) {// check whether a single character matches
First = false; // re-match the first character mark of temp
Offset = 0; // reset the offset to 0.
}
}
++ II;
}

If (offset = Len) {// match successful
Cout <"the result:" <temp <Endl;
Return true;
}
Else
Return false;
}

Int main (){
 
Char CHS [] = "fesabcdsfsfdcbasf ";
Const int Len = strlen (CHS );
Cout <"Len:" <Len <Endl;
Char RCHS [Len + 1];

// Reverse Order
Reverse (CHS, RCHS, Len );

Cout <CHS <Endl;
Cout <RCHS <Endl;

Int middle = Len/2;
Bool flag = true;
For (INT I = middle; I> 0 & flag; -- I ){

For (Int J = 0; j <= (middle-I) & flag; ++ J ){
Char temp [Len + 1];
Getchs (temp, CHS, J, I );
// Flag = compare ("SABC", "asabccccc", 4, 5 );
Flag =! Compare (temp, RCHS, I, len-middle );
}
}
System ("pause ");
Return 1;
}

In this example, the largest forward string is obtained when the positive-order string S and reverse-order string SR of a string CHS coexist.

 

Idea: 1. For example, if the string CHS = 'fesabcdsfdcbasf ", the reverse string RCHS =" fsabcdfsfsdcbasef ";

2. Because S and Sr are not covered, the first part of CHS can be used to match the first part of RCHS;

3. The length of CHS is 17, so you can match the first eight strings with RCHS at most, and match only the first (17-8) strings of RCHS.

4. The temp: fesabcds
The temp: fesabcd
The temp: esabcds
The temp: fesabc
The temp: esabcd
The temp: sabcds

Similarly, the length of the matched string is gradually reduced until the matching is successful.

 

 

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.