topcoder题目及我的程序(4)——find reversed string (算法1)

Problem Statement
????
You are given a String input. You are to find the longest substring of input such that 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 is 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 will contain between 1 and 50 characters, inclusive.
-
Each character of input will be an uppercase letter ('A'-'Z').
Examples
0)

????
"XBCDEFYWFEDCBZ"
Returns: "BCDEF"
We see that the reverse of BCDEF is FEDCB, which appears later in the string.
1)

????
"XYZ"
Returns: "X"
The best we can do is find a one character substring, so we implement the tie-breaker rule of taking the earliest one first.
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 is its own reversal.

This problem statement is 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 prohibited. (c)2003, TopCoder, Inc. All rights reserved.

题解

在一个字符串s中找到最长的第一次出现的子串s1,s1满足:s1的反串s2也是s的子串。
算法1:先找到所有这样的子串,再在其中找到最长的

源程序如下:

/************************************************************************
 * You are given a String input. You are to find the longest substring of
 * input such that 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 
< VECTOR >

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
=input.substr(start,sublen);
            m_vecSubstring.push_back(sub);

            i
+=i-start+1;
            j
=len-1;
        }

        
else if(i<j)    //"ABCDWFDCBZ"
        {
            sublen
=i-start;
            sub
=input.substr(start,sublen);
            m_vecSubstring.push_back(sub);

            i
--;
            j
--;
        }

        
        start
=i;
    }

}


// get the longest valid substring
string  ReverseSubstring::GetLongestSubstring()
{
    
int index,maxLen,templen;

    index
=0;
    maxLen
=m_vecSubstring.at(0).size();

    
//printf("   substring : %s ",m_vecSubstring.at(0).c_str());

    
for(int i=1;i<m_vecSubstring.size();i++)
    
{
        
//printf("  substring : %s ",m_vecSubstring.at(i).c_str());

        templen
=m_vecSubstring.at(i).size();
        
if(templen>maxLen)
        
{
            index
=i;
            maxLen
=templen;
        }

    }


    
return m_vecSubstring.at(index);
}


// find the reversed substring meeting the demand
string  ReverseSubstring::findReversed( string  input)
{
    GetAllValidSubstrings(input);
    
return GetLongestSubstring();
}


// 显示菜单
void  show_menu()
{
    printf(
"--------------------------------------------- ");
    printf(
"input command to test the program ");
    printf(
"   i or I : input n to test ");
    printf(
"   q or Q : quit ");    
    printf(
"--------------------------------------------- ");
    printf(
"$ input command >");
}


void  main()
{
    
char sstr[50],sinput[10];

    show_menu();

    scanf(
"%s",sinput);
    
while(stricmp(sinput,"q")!=0)
    
{
        
if(stricmp(sinput,"i")==0)
        
{
            printf(
"  please input a string:");
            scanf(
"%s",sstr);

            
string str(sstr);
            ReverseSubstring reverse;
            
string substr=reverse.findReversed(str);

            printf(
"   source  string : %s   longest valid substring: %s ",str.c_str(),substr.c_str());
        }


        
//输入命令
        printf("$ input command >");
        scanf(
"%s",sinput);
    }

}

 

运行结果如下:

 topcoder题目及我的程序(4)——find reversed string (算法1)_第1张图片

 

你可能感兴趣的:(算法,String,vector,input,constraints,character)