最大回文串

题目链接:https://www.nowcoder.com/practice/12e081cd10ee4794a2bd70c7d68f5507?tpId=37&tqId=21308&tPage=5&rp=&ru=/ta/huawei&qru=/ta/huawei/question-ranking

参考:https://blog.csdn.net/CSDN_FengXingwei/article/details/82429808

#include 
#include 
#include 
using namespace std;


int getMaxPlalindrome(string s)
{
	int len = s.size();
	int start, end;
	vector> dp; 
	vector temp;
	for (int i = 0; i < len; i++)
	{
		temp.clear();
		for (int j = 0; j < len; j++)
		{
			temp.push_back(false);
		}
		dp.push_back(temp);
	}
	for (int i = 0; i < len; i++)
	{
		for (int j = 0; j < len - i; j++)
		{
			if (i == 0)
			{
				dp[j][j + i] = true;
			}
			else if (i == 1)
			{
				dp[j][j + i] = true;
			}
			else
			{
				if (dp[j + 1][j + i - 1] && (s[j] == s[j + i]))
					dp[j][j + i] = true;
			}
			if (dp[j][j + i] == true)
			{
				start = j;
				end = j + i;
			}
		}
	}
	return end - start + 1;
}

int main()
{
	string s;
	while (cin >> s)
	{
		cout << getMaxPlalindrome(s) << endl;
	}
	
}

 

你可能感兴趣的:(华为牛客网刷题)