【十二题解】 | #最长回文子串#
最长回文子串
http://www.nowcoder.com/practice/12e081cd10ee4794a2bd70c7d68f5507
穷举了属于是
#include<stdio.h>
int f_max(int a, int b){
return a>b?a:b;
}
int main() {
char data[350];
char* loc = data;
while (scanf("%c", loc) != EOF)loc++;
int lenth = (loc - data) / sizeof(char) - 1;
int** dp = (int**)malloc(sizeof(int*) * lenth);
for (int i = 0; i < lenth; i++) {
dp[i] = (int*)malloc(sizeof(int) * lenth);
memset(dp[i], 1, sizeof(int));
}
for (int i = lenth-1; i >= 0; i--) {
for (int j = 0; j < lenth; j++) {
int lenth = j - i + 1;
if (lenth < 1) {
dp[i][j] = 0;
}
else if (lenth == 1 || (lenth == 2 && data[i] == data[j])) {
dp[i][j] = lenth;
}
else {
if (dp[i + 1][j - 1] != 0 && (data[i] == data[j]))
{
dp[i][j] = lenth;
}
else {
dp[i][j] = 0;
}
}
}
}
int mmax = 0;
for (int i = 0; i < lenth; i++) {
for (int j = 0; j < lenth; j++) {
mmax = f_max(mmax, dp[i][j]);
}
}
printf("%d\n", mmax);
}