最大回文子串长度 [dp]

dp 将复杂度降到n平方
在这里插入图片描述

#include<iostream>
#include<vector>
#include<queue>
#include<stack>
#include<string>
#include<math.h>
#include<algorithm>
#include<map>
#include<cstring>
#include<set>
using namespace std;
bool dp[100][100];
int main()
{
	string s;
	cin >> s;
	int maxn = s.size();
	for (int i = 0; i < maxn; i++)
	{
		dp[i][i] = true;
	}
	int max = 1;
	for (int j = 1; j < maxn; j++)
	{
		for (int i = 0; i < maxn - 1&&i<j; i++)
		{
			if (s[i] != s[j])
			{
				dp[i][j] = false;
			}
			else
			{
				if (j - i < 3)
				{
					dp[i][j] = true;
				}
				else
				{
					dp[i][j] = dp[i + 1][j - 1];
				}
			}
			if (dp[i][j] && j - i + 1 > max)
			{
				max = j - i + 1;
			}
		}
	}
	cout << max << endl;
}

原文地址:https://www.cnblogs.com/Hsiung123/p/13811987.html