对称子字符串的最大长度
2014-06-09 15:26
253 查看
输入一个字符串,输出该字符串中对称的子字符串的最大长度。比如输入字符串“google”,由于该字符串里最长的对称子字符串是“goog”,因此输出4。
提示:可能很多人都写过判断一个字符串是不是对称的函数,这个题目可以看成是该函数的加强版。
找出一个字符串中的最长奇数回文。在控制台输入输出。
#include <iostream>
#include <string>
using namespace std;
bool isPalindromicSubStr(const string &s,int start,int end)
{
if(s.length() == 0 || s.length() == 1)
return true;
while(start < end)
{
if(s[start] != s[end])
{
return false;
}
start++;
end--;
}
return true;
}
int longestPalindromLength(const string &s)
{
int maxLength = 0;
for(int i = 0;i<s.length();i++)
{
for(int j = 0;j<s.length();j++)
{
if(isPalindromicSubStr(s,i,j))
{
int len = j-i+1;
if(len > maxLength)
{
maxLength = len;
}
}
}
}
return maxLength;
}
int dplongestPalindromLength(const string &s)
{
if(s.length() == 0)
return 0;
if(s.length() == 1)
return 1;
int len = s.length();
bool **dp = new bool *[len];
for(int i = 0;i<len;i++)
dp[i] = new bool[len];
for(int i = 0;i<len;i++)
for(int j = 0;j<len;j++)
{
if (i>=j)
{
dp[i][j] = true;
}
else
{
dp[i][j] = false;
}
}
int maxLen = 0;
for(int k = 1;k<len;k++)
{
for(int i = 0;i+k<len;i++)
{
int j = i + k;
if(s[i] != s[j])
{
dp[i][j] = false;
}
else
{
dp[i][j] = dp[i+1][j-1];
if(dp[i][j])
{
if(k+1 > maxLen)
{
maxLen = k+1;
}
}
}
}
}
return maxLen;
}
int main()
{
string s = "google";
cout << "BrutefORCE"<<longestPalindromLength(s)<<endl;
cout << "dp"<<dplongestPalindromLength(s)<<endl;
system("pause");
return 0;
}
提示:可能很多人都写过判断一个字符串是不是对称的函数,这个题目可以看成是该函数的加强版。
找出一个字符串中的最长奇数回文。在控制台输入输出。
#include <iostream>
#include <string>
using namespace std;
bool isPalindromicSubStr(const string &s,int start,int end)
{
if(s.length() == 0 || s.length() == 1)
return true;
while(start < end)
{
if(s[start] != s[end])
{
return false;
}
start++;
end--;
}
return true;
}
int longestPalindromLength(const string &s)
{
int maxLength = 0;
for(int i = 0;i<s.length();i++)
{
for(int j = 0;j<s.length();j++)
{
if(isPalindromicSubStr(s,i,j))
{
int len = j-i+1;
if(len > maxLength)
{
maxLength = len;
}
}
}
}
return maxLength;
}
int dplongestPalindromLength(const string &s)
{
if(s.length() == 0)
return 0;
if(s.length() == 1)
return 1;
int len = s.length();
bool **dp = new bool *[len];
for(int i = 0;i<len;i++)
dp[i] = new bool[len];
for(int i = 0;i<len;i++)
for(int j = 0;j<len;j++)
{
if (i>=j)
{
dp[i][j] = true;
}
else
{
dp[i][j] = false;
}
}
int maxLen = 0;
for(int k = 1;k<len;k++)
{
for(int i = 0;i+k<len;i++)
{
int j = i + k;
if(s[i] != s[j])
{
dp[i][j] = false;
}
else
{
dp[i][j] = dp[i+1][j-1];
if(dp[i][j])
{
if(k+1 > maxLen)
{
maxLen = k+1;
}
}
}
}
}
return maxLen;
}
int main()
{
string s = "google";
cout << "BrutefORCE"<<longestPalindromLength(s)<<endl;
cout << "dp"<<dplongestPalindromLength(s)<<endl;
system("pause");
return 0;
}
相关文章推荐
- 【算法25】对称子字符串的最大长度
- 对称子字符串的最大长度
- 数据结构练习(34)对称子字符串的最大长度
- 对称子字符串的最大长度
- 对称子字符串最大长度
- 对称子字符串的最大长度
- 对称子字符串的最大长度[算法]
- 程序员面试题精选100题(46)-对称子字符串的最大长度
- 面试46题 对称子字符串最大长度
- 对称子字符串的最大长度
- 对称子字符串的最大长度
- 程序员面试100题之四十六,对称子字符串的最大长度
- 对称子字符串的最大长度
- 对称子字符串的最大长度
- 面试训练对称子字符串的最大长度
- 对称子字符串的最大长度
- 对称子字符串的最大长度
- 对称子字符串的最大长度
- 程序员面试题精选100题(46)-对称子字符串的最大长度
- 算法题36 对称子字符串的最大长度