Chinaunix首页 | 论坛 | 博客
  • 博客访问: 121986
  • 博文数量: 53
  • 博客积分: 0
  • 博客等级: 民兵
  • 技术积分: 620
  • 用 户 组: 普通用户
  • 注册时间: 2014-08-24 16:22
文章存档

2014年(53)

我的朋友

分类: C/C++

2014-10-07 15:06:53

Given a string S, find the longest palindromic substring in S. You may assume that the maximum length of S is 1000, and there exists one unique longest palindromic substring.

首先我用了DP,没想到存储没报错,时间报错了,TLE了。

原理是记录所有回文,然后更新最大值。

比较值得注意的是
  1. for(int i=n-2;i>=0;i--){
  2.         for(int j=i+1;j<=n-1;j++){
  3.             dp[i][j]=dp[i+1][j-1]&&(s[i]==s[j]);
i依赖于i+1, j依赖于j-1,所以i--,j++的遍历

  1. string longestPalindrome(string s) {
  2.     int n=s.length();
  3.     vector<vector<bool>> dp(n,vector<bool>(n,false));
  4.     for(int i=0;i<n;i++){
  5.         for(int j=0;j<=i;j++){
  6.             dp[i][j]=true;
  7.         }
  8.     }
  9.     int num=1;
  10.     int x=0;
  11.     int y=0;
  12.     for(int i=n-2;i>=0;i--){
  13.         for(int j=i+1;j<=n-1;j++){
  14.             dp[i][j]=dp[i+1][j-1]&&(s[i]==s[j]);
  15.             if(dp[i][j] && (j-i+1>num)){
  16.                 num=j-i+1;
  17.                 x=i;
  18.                 y=j;
  19.             }
  20.         }
  21.     }
  22.     return s.substr(x,num);
  23. }
后来发现其实朴素的判断回文空间复杂性更小。其实就是从0到n-1遍历,每个节点为中心判断回文,分为奇偶两种情况,

奇数遍历:


  1. for (int i = 1; i < s.length(); ++i){
  2.         low = i - 1;
  3.         high = i + 1;
  4.         while (low >= 0 && high <s.length() && s[low] == s[high])
  5.         {
  6.             //是回文
  7.             --low;
  8.             ++high;
  9.         }
  10. }



数遍历:


  1. for (int i = 1; i < s.length(); ++i){
  2.         low = i - 1;
  3.         high = i;
  4.         while (low >= 0 && high <s.length() && s[low] == s[high])
  5.         {
  6.             //是回文
  7.             --low;
  8.             ++high;
  9.         }
  10. }



代码如下:

  1. string longestPalindrome(string s) {

  2. int start = 0;
  3.  int maxLength = 1;
  4. string res;

  5.     if(s.length()==1)
  6.        return s;
  7.     int low, high;
  8.     for (int i = 1; i < s.length(); ++i)
  9.     {
  10.         low = i - 1;
  11.         high = i;
  12.         while (low >= 0 && high < s.length() && s[low] == s[high])
  13.         {
  14.             if (high - low + 1 > maxLength)
  15.             {
  16.                 start = low;
  17.                 maxLength = high - low + 1;
  18.             }
  19.             --low;
  20.             ++high;
  21.         }
  22.          low = i - 1;
  23.         high = i + 1;
  24.         while (low >= 0 && high <s.length() && s[low] == s[high])
  25.         {
  26.             if (high - low + 1 > maxLength)
  27.             {
  28.                 start = low;
  29.                 maxLength = high - low + 1;
  30.             }
  31.             --low;
  32.             ++high;
  33.         }
  34.     }
  35.      res = s.substr(start,maxLength);
  36. return res;


  37. }

阅读(921) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~