鸟食轩

 Microsoft .NET[C#] MVP 2003
随笔 - 424, 文章 - 233, 评论 - 5417, 引用 - 344
数据加载中……

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

posted on 2007-01-16 23:49 birdshome 阅读(1666) 评论(5)  编辑 收藏 所属分类: 其它编程相关内容

评论

#1楼    回复  引用    

Hi,birdshome,我也写了一个,看上去似乎更简单一点。
//不过我没有学过C#,也许语法上有问题
private static string FindReversed(string input)
{
int i, j, k, m = 0, len = 1, c = input.Length;
for (i = 0; i < c - len; ++i) {
for (j = c; --j > len;) {
for (k = 0; k < c - i && k <= j && input[i + k] == input[j - k]; k++);
if (k > len) {
len = k;
m = i;
}
}
}
return input.Substring(m, len);
}
2007-01-22 10:31 | modico [未注册用户]

#2楼 [楼主]   回复  引用  查看    

@modico
It's very sexy code:)
2007-01-31 10:29 | birdshome      

标题  
姓名  
主页
Email (博主才能看到) 
验证码 *  看不清,换一张 [登录][注册]
内容(请不要发表任何与政治相关的内容)  
  登录  使用高级评论  新用户注册  返回页首  恢复上次提交      
该文被作者在 2007-01-20 17:30 编辑过


相关链接:

历史上的今天:
2006-01-16 1234567890
2005-01-16 关于HTML Object中三个Style实例的区别