HDU 1513 Add minimum palindrome (Lcs scrolling array)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.