Hdu-4745-Two Rabbits

Source: Internet
Author: User

Question: two rabbits jumped on n blocks of a ring Stone. Each stone had a weight of ai. One jumped from left to right, and the other jumped from right to left, for each hop, the weights of the stones where the two rabbits are located must be equal and within the same circle (each of them cannot exceed its own starting point or return to its starting point again) the maximum number of stones they can pass (1 <= n <= 1000, 1 <= ai <= 1000 ). Question link: http://acm.hdu.edu.cn/showproblem.php? Pid = 4745 --> after the simulation sample, the first look is like the Euler loop, then the students say it should be the longest public subsequence LCS, and then it's miserable until the game's Ended all TLE ...... Originally, it was just a simple dp to find the longest echo sub-sequence ...... Suppose a sequence with 11 numbers: 1 2 3 4 3 2 1 8 9 9 8 suppose that after 7th numbers, the first 7 are a back-to-text sequence, the last four are also a return sequence, so we may try to start from the center of the Left return string, a clockwise, a counter-clockwise. After simulation, we will find that the answer is: enumeration tangent, the maximum length of the left-side echo sub-sequence plus the maximum length of the right-side echo sub-sequence.

#include <cstdio>  #include <algorithm>    using namespace std;    const int maxn = 1000 + 10;    int n, a[maxn], d[maxn][maxn];    void read(){      for(int i = 1; i <= n; i++) scanf("%d", &a[i]);  }    void solve(){      for(int i = 1; i <= n; i++) d[i][i] = 1;      for(int len = 2; len <= n; len++)          for(int i = 1; i <= n-len+1; i++){              int j = i + len - 1;              if(a[i] == a[j]) d[i][j] = d[i+1][j-1] + 2;              else d[i][j] = max(max(d[i+1][j], d[i][j-1]), d[i+1][j-1]);          }      int Max = -1;      for(int i = 1; i <= n; i++) Max = max(Max, d[1][i] + d[i+1][n]);      printf("%d\n", Max);  }    int main()  {      while(scanf("%d", &n) == 1 && n){          read();          solve();      }      return 0;  }  

 


Related Article

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.