Topic Portal
1 /*2 LCS nude title: length minus the maximum same length is the number of inserts to be inserted3 DP Array Two-dimensional open 5000 will be super memory, here is used to scroll the array,4 because in the calculation of LCS, I changes only 1, so you can scroll by 2 to take the remainder:)5 */6#include <cstdio>7#include <iostream>8#include <cstring>9#include <algorithm>Ten#include <string> One using namespacestd; A - Const intMAXN = 5e3 +Ten; - Const intINF =0x3f3f3f3f; the intdp[2][MAXN]; - strings, SS; - - intLCS (intN) + { - intx =0; + for(intI=1; i<=n; ++i) A { atx =1-x; - for(intj=1; j<=n; ++j) - { - if(ss[i-1] = = s[j-1]) - { -DP[X][J] = dp[1-x][j-1] +1; in } - Else to { +DP[X][J] = max (dp[x][j-1], dp[1-X] [j]); - } the } * } $ Panax Notoginseng returnDp[x][n]; - } the + intMainvoid)//POJ 1159 palindrome A { the #ifndef Online_judge +Freopen ("1159.in","R", stdin); - #endif $ $ intN; - while(~SCANF ("%d", &N)) - { theMemset (DP,0,sizeof(DP)); -CIN >>SS;Wuyis =SS; the Reverse (S.begin (), S.end ()); - Wu intres =LCS (n); -printf ("%d\n"Nres); About } $ - return 0; -}
LCS (scrolling array) POJ 1159 palindrome