http://blog.csdn.net/ice_crazy/article/details/8244639
This 5000*5000 out of memory, so you need to use a scrolling array:
Using a now to represent the current result, the pre represents the previous result, and keeps scrolling
#include <iostream>#include<string>#include<cstring>#include<cstdlib>#include<cstdio>#include<cmath>#include<algorithm>#include<stack>#include<queue>#include<cctype>#include<vector>#include<iterator>#include<Set>#include<map>#include<sstream>using namespacestd;#defineMem (A, B) memset (A,b,sizeof (a))#definePF printf#defineSF scanf#defineSPF sprintf#definePB Push_back#defineDebug printf ("!\n")#defineMAXN 5005#defineMAX (A, b) a>b?a:b#defineBlank pf ("\ n")#defineLL Long Long#defineAll (x) X.begin (), X.end ()#defineINS (x) Inserter (X,x.begin ())#definePqueue priority_queue#defineINF 0x3f3f3f3fintn,m;CharA[MAXN],B[MAXN];intdp[2][MAXN];intLCsintal) { inti,j; intNow,pre; for(i=1; i<=al;i++) { for(j=1; j<=al;j++) { now= i%2; Pre=1-Now ; if(a[i-1] = = b[j-1]) Dp[now][j] = dp[pre][j-1]+1; ElseDP[NOW][J] = max (dp[pre][j],dp[now][j-1]); } } returnal-dp[al%2][al];}intMain () {inti,j; while(~SF ("%d",&N)) {mem (DP,0); SF ("%s", a); strcpy (B,a); Strrev (b); PF ("%d\n", LCS (n)); }}
HDU 1513 Add minimum palindrome (Lcs scrolling array)