HDU 4745 interval DP

Source: Internet
Author: User

Test instructions: The longest palindrome sequence of a ring, is a sequence not a string

Link: Point Me

The starting point can be arbitrary,

So only the longest palindrome sequence of each interval is required to take Max (Dp[1][i]+dp[i+1][n]), the final answer can be

Originally wanted to expand twice times, but later the biggest not too good to think

    1. Multiply the ring into a chain, find the window is the eldest of N sequence, but this is not the final solution, you can try to see the sample 2, is only 4, because it can be selected in the palindrome outside also select as a starting point to jump, so the outside to determine whether the palindrome outside can also be the starting point of the stone, that can find the window (n-1) of length +1. So the solution is to find the length of the window n or the maximum value of the window (n-1) length +1.

    2. Do not multiply, directly as a chain to seek DP, and then cut into two halves, to find the length of the palindrome on both sides, the largest and is the solution. There is no need to consider the starting point, because both sides of the palindrome can do the starting point.

Sample Input

1141 1 2 162 1 1 2 1 30
Sample Output
145



2015-05-06 Code
1#include <cstdio>2#include <iostream>3#include <algorithm>4#include <cstring>5#include <cmath>6#include <queue>7#include <map>8 using namespacestd;9 #defineMOD 1000000007Ten Const intinf=0x3f3f3f3f; One Const Doubleeps=1e-5; AtypedefLong Longll; - #defineCL (a) memset (A,0,sizeof (a)) - #defineTS printf ("*****\n"); the Const intmaxn=1005; - intN,m,tt; - intA[MAXN]; - intDP[MAXN][MAXN]; + intMain () - { +     inti,j,k; A #ifndef Online_judge atFreopen ("1.in","R", stdin); -     #endif -      while(SCANF ("%d", &n)! =EOF) -     { -         if(n==0) Break; - CL (DP); in          for(i=0; i<n;i++) -         { toscanf"%d",&a[i]); +dp[i][i]=1; -         } the          for(intlen=1; len<n;len++) *         { $              for(i=0; i+len<n;i++)Panax Notoginseng             { -j=len+i; the                 if(A[i]==a[j]) dp[i][j]=dp[i+1][j-1]+2; +                 ElseDp[i][j]=max (dp[i][j-1],dp[i+1][j]); A             } the         } +         intcn1=0; -          for(intI=0; i<n;i++) $Ans=max (ans,dp[0][i]+dp[i+1][n-1]); $printf"%d\n", ans); -     } -}
View Code

1#include <cstdio>2#include <algorithm>3#include <cstring>4 using namespacestd;5 intdp[1001][1001];6 inta[1001];7 intN;8 intMain ()9 {Ten      while(SCANF ("%d", &n) &&N) One     { A          for(intI=1; i<=n;i++) -scanf"%d",&a[i]); -Memset (DP,0,sizeof(DP)); the          for(intI=1; i<=n;i++) -dp[i][i]=1; -          for(intlen=1; len<n;len++) { -              for(intI=1; i+len<=n;i++) { +                 intj=i+Len; -                 if(a[i]==A[j]) +dp[i][j]=dp[i+1][j-1]+2; A                 Else atDp[i][j]=max (dp[i][j-1],dp[i+1][j]); -             } -         } -         intcn1=0; -          for(intI=1; i<=n;i++) -Ans=max (ans,dp[1][i]+dp[i+1][n]); inprintf"%d\n", ans); -     } to     return 0; +}

HDU 4745 interval DP

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.