TopCoder topic and my Program (4)--find reversed string (algorithm 1) __ algorithm

Source: Internet
Author: User
Tags uppercase letter valid

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&GT;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=

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.