Code: Search a Reverse Substring

简介:

 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.

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"
    The similar as above.
4) "ABCDCBA" 
    Returns: "ABCDCBA" 
    Here, the entire string is its own reversal.

class  ReverseSubstring
{
    static void Main(string
[] args)
    
{
        string[] strings = new string[] 
{ "XBCDEFYWFEDCBZ", "XYZ"
                "FDASJKUREKJFDFASIREYUFDHSAJYIREWQ", "ABCDCBA" }
;
        for ( int i=0 ; i < strings.Length ; ++
i )
        
{
            System.Console.WriteLine(FindReversed(strings[i]));
        }

    }

    
private static string FindReversed(string  input)
    
{
        string substring = string
.Empty;
        for ( int i=0 ; i < input.Length ; ++
i )
        
{
            for ( int j=0 ; j < input.Length ; ++
j )
            
{
                if ( input[i] == input[input.Length-1-
j] )
                
{
                    if ( i == input.Length-1-
j )
                    
{
                        if ( substring.Length == 0
 )
                        
{
                            substring =
 input[i].ToString();
                        }

                         break ;
                    }

                     else
                     {
                        int k = 0
;
                        for ( ; k < input.Length-i-j ; ++
k )
                        
{
                            if ( input[i+k] != input[input.Length-1-j-k] ) break
;
                        }

                        if ( substring.Length <  k )
                        
{
                            substring =
 input.Substring(i, k);
                        }

                    }
                }
            }
        }
         return  substring;
    }

}

Results are:
BCDEF
X
ABA
FDF
ABCDCBA

本文转自博客园鸟食轩的博客,原文链接:http://www.cnblogs.com/birdshome/,如需转载请自行联系原博主。

目录
相关文章
4.1、Array数组常用的方法(map、push、sort、filter、join、split)
4.1、Array数组常用的方法(map、push、sort、filter、join、split)
154 0
|
机器学习/深度学习 人工智能
(case when length(p.name)>25 then concat(substring(p.name,1,12),'...') else p.name end) as projectName
SELECT DISTINCT pw.project_id as projectId , (case when length(p.name)>25 then concat(substring(p.
1078 0
reverse 函数 与 substring_index 函数
select reverse(substring_index(reverse(substring_index((select dept_name from department where dept_no=20862), '-', 2)), '-', ...
1103 0