Problem Statement
????
You is given a String input. You were to find the longest substring of input such, the reversal of the substring is also a substring of input. In case of a tie, return the string that occurs earliest in input.
Definition
????
Class:
Reversesubstring
Method:
Findreversed
Parameters:
String
Returns:
String
Method Signature:
String findreversed (String input)
(Be sure your method was public)
????
Notes
-
The substring and its reversal may overlap partially or completely.
-
The entire original string is itself a valid substring (see example 4).
Constraints
-
Input would contain between 1 and characters, inclusive.
-
Each character of input is A uppercase letter (' A '-' Z ').
Examples
0)
????
"XBCDEFYWFEDCBZ"
Returns: "Bcdef"
We See the reverse of Bcdef are FEDCB, which appears later in the string.
1)
????
"XYZ"
Returns: "X"
The best we can do are find a one character substring, so we implement the Tie-breaker rule of taking the earliest one firs T.
2)
????
"Abcaba"
Returns: "ABA"
The string ABA is a palindrome (it's its own reversal), so it meets the criteria.
3)
????
"Fdasjkurekjfdfasireyufdhsajyirewq"
Returns: "FDF"
4)
????
"ABCDCBA"
Returns: "ABCDCBA"
Here, the entire string was its own reversal. This problem statement are the exclusive and proprietary property of TopCoder, Inc. Any unauthorized use or reproduction of this information without the prior written consent of TopCoder, Inc. is strictly p Rohibited. (c) 2003, TopCoder, Inc. All rights reserved.
The puzzle finds the longest first occurrence of the substring in a string s s1,s1 satisfies: S1 crossdress S2 is also a substring of s. Algorithm 1: Find all of these substrings first, and then find the longest
The source program is as follows:/**//************************************************************************
* You are given a String input. You is to find the longest substring of
* Input such the reversal of the substring is also a substring of
* Input. In case of a tie, return the string that occurs earliest in input
************************************************************************/
#include < stdio.h >
#include < STRING >
#include < vectors >
using namespace Std;
Class Reversesubstring
... {
Public
All valid substrings
Vector <string> m_vecsubstring;
Public
Reversesubstring () ... {}
~reversesubstring () ... {}
void Getallvalidsubstrings (string input);
String getlongestsubstring ();
String findreversed (string input);
} ;
Find all valid substrings
void Reversesubstring::getallvalidsubstrings (String input)
... {
int i,j,len,start,sublen=1;
String Sub;
M_vecsubstring.clear ();
Len=input.size ();
i=0;
j=len-1;
start=i;
while (I<len)
... {
while (input.at (i)!=input.at (j))
j--;
while (input.at (i) ==input.at (j) && I<j)
... {
i++;
j--;
}
if (I>J)//"BCDDCB"
... {
sublen=2* (I-start);
Sub=input.substr (Start,sublen);
M_vecsubstring.push_back (sub);
I+=i-start;
j=len-1;
}
else if (i==j)//"BCDEDCB"
... {
sublen=2* (I-start) +1;
sub=