正文

Google中国赛代码参详Group22-250分(1)2005-12-13 11:28:00

【评论】 【打印】 【字体: 】 本文链接:http://blog.pfan.cn/ddtme/8145.html

分享到:

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.

//代码:

///////////////////////////////////////////////////////////////////////////////////////////////

#include
#include

using namespace std;


class ReverseSubstring
{
public:
 string findReversed(string input)
 {
  string res,temp;
  int len;
  bool flag;
  len = 0;
  for(int i=0;i<(int)input.size();i++)
  {
   for(int j = 1;j<=(int)(input.size()-i);j++)
   {
    temp = input.substr(i,j);
    flag = isPD(temp,input);
    if(flag)
    {
     if(len<(int)temp.size())
     {
      res = temp;
      len= res.size();
     }
    }
   }
  }
  if(len == 0)
  {
   len = 1;
   res = input.substr(0,1);
  }
  return res;
 };

 string resever(string a)
 {
  string b;
  for(int i=0;i<(int)a.size();i++)
  {
   b.insert(0,1,a[i]);
  }
  return b;
 };
 bool isPD(string c,string in)
 {
  string t;
  c = resever(c);
  for(int i = 0;i<=(int)(in.size()-c.size());i++)
  {
   t = in.substr(i,c.size());
   if(t == c)
   {
    return true;
   }
  }
  return false;
 };
};

 


int main()
{
 string input,output;
 ReverseSubstring revSubstr;
 while(true)
 {
  cin>>input;
  output = revSubstr.findReversed(input);
  cout< }
}
/*
XBCDEFYWFEDCBZ
XYZ
ABCABA
FDASJKUREKJFDFASIREYUFDHSAJYIREWQ
ABCDCBA

*/

阅读(4041) | 评论(0)


版权声明:编程爱好者网站为此博客服务提供商,如本文牵涉到版权问题,编程爱好者网站不承担相关责任,如有版权问题请直接与本文作者联系解决。谢谢!

评论

暂无评论
您需要登录后才能评论,请 登录 或者 注册