zoukankan      html  css  js  c++  java
  • HDU 4745 Two Rabbits (2013杭州网络赛1008,最长回文子串)

    Two Rabbits

    Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)
    Total Submission(s): 505    Accepted Submission(s): 260


    Problem Description
    Long long ago, there lived two rabbits Tom and Jerry in the forest. On a sunny afternoon, they planned to play a game with some stones. There were n stones on the ground and they were arranged as a clockwise ring. That is to say, the first stone was adjacent to the second stone and the n-th stone, and the second stone is adjacent to the first stone and the third stone, and so on. The weight of the i-th stone is ai.

    The rabbits jumped from one stone to another. Tom always jumped clockwise, and Jerry always jumped anticlockwise.

    At the beginning, the rabbits both choose a stone and stand on it. Then at each turn, Tom should choose a stone which have not been stepped by itself and then jumped to it, and Jerry should do the same thing as Tom, but the jumping direction is anti-clockwise.

    For some unknown reason, at any time , the weight of the two stones on which the two rabbits stood should be equal. Besides, any rabbit couldn't jump over a stone which have been stepped by itself. In other words, if the Tom had stood on the second stone, it cannot jump from the first stone to the third stone or from the n-the stone to the 4-th stone.

    Please note that during the whole process, it was OK for the two rabbits to stand on a same stone at the same time. 

    Now they want to find out the maximum turns they can play if they follow the optimal strategy.
     
    Input
    The input contains at most 20 test cases.
    For each test cases, the first line contains a integer n denoting the number of stones.
    The next line contains n integers separated by space, and the i-th integer ai denotes the weight of the i-th stone.(1 <= n <= 1000, 1 <= ai <= 1000)
    The input ends with n = 0.
     
    Output
    For each test case, print a integer denoting the maximum turns.
     
    Sample Input
    1 1 4 1 1 2 1 6 2 1 1 2 1 3 0
     
    Sample Output
    1 4 5
    Hint
    For the second case, the path of the Tom is 1, 2, 3, 4, and the path of Jerry is 1, 4, 3, 2. For the third case, the path of Tom is 1,2,3,4,5 and the path of Jerry is 4,3,2,1,5.
     
    Source
     
    Recommend
    liuyiding
     

    答案竟然就是分成两部分以后的最长回文子串,

    太难想到了,TAT

     1 /* ***********************************************
     2 Author        :kuangbin
     3 Created Time  :2013/9/15 星期日 15:20:03
     4 File Name     :2013杭州网络赛1008.cpp
     5 ************************************************ */
     6 
     7 #pragma comment(linker, "/STACK:1024000000,1024000000")
     8 #include <stdio.h>
     9 #include <string.h>
    10 #include <iostream>
    11 #include <algorithm>
    12 #include <vector>
    13 #include <queue>
    14 #include <set>
    15 #include <map>
    16 #include <string>
    17 #include <math.h>
    18 #include <stdlib.h>
    19 #include <time.h>
    20 using namespace std;
    21 #define REP(I, N) for (int I=0;I<int(N);++I)
    22 #define FOR(I, A, B) for (int I=int(A);I<int(B);++I)
    23 #define DWN(I, B, A) for (int I=int(B-1);I>=int(A);--I)
    24 #define REP_1(I, N) for (int I=1;I<=int(N);++I)
    25 #define FOR_1(I, A, B) for (int I=int(A);I<=int(B);++I)
    26 #define DWN_1(I, B, A) for (int I=int(B);I>=int(A);--I)
    27 #define REP_C(I, N) for (int N____=int(N),I=0;I<N____;++I)
    28 #define FOR_C(I, A, B) for (int B____=int(B),I=A;I<B____;++I)
    29 #define DWN_C(I, B, A) for (int A____=int(A),I=B-1;I>=A____;--I)
    30 #define REP_1_C(I, N) for (int N____=int(N),I=1;I<=N____;++I)
    31 #define FOR_1_C(I, A, B) for (int B____=int(B),I=A;I<=B____;++I)
    32 #define DWN_1_C(I, B, A) for (int A____=int(A),I=B;I>=A____;--I)
    33 #define DO(N) while(N--)
    34 #define DO_C(N) int N____ = N; while(N____--)
    35 #define TO(i, a, b) int s_=a<b?1:-1,b_=b+s_;for(int i=a;i!=b_;i+=s_)
    36 #define TO_1(i, a, b) int s_=a<b?1:-1,b_=b;for(int i=a;i!=b_;i+=s_)
    37 #define SQZ(I, J, A, B) for (int I=int(A),J=int(B)-1;I<J;++I,--J)
    38 #define SQZ_1(I, J, A, B) for (int I=int(A),J=int(B);I<=J;++I,--J)
    39 
    40 const int MAXN = 1010;
    41 int a[MAXN];
    42 int dp[MAXN][MAXN];
    43 int main()
    44 {
    45     //freopen("in.txt","r",stdin);
    46     //freopen("out.txt","w",stdout);
    47     int n;
    48     while(scanf("%d",&n) ==1 && n)
    49     {
    50         for(int i = 1;i <= n;i++)
    51             scanf("%d",&a[i]);
    52         memset(dp,0,sizeof(dp));
    53         for(int i = 1;i <= n;i++)dp[i][i] = 1;
    54         for(int k = 1;k <= n;k++)
    55             for(int i = 1;i + k <= n;i++)
    56             {
    57                 dp[i][i+k] = max(dp[i+1][i+k],dp[i][i+k-1]);
    58                 if(a[i] == a[i+k])dp[i][i+k] = max(dp[i][i+k],2+dp[i+1][i+k-1]);
    59             }
    60         int ans = 0;
    61         for(int i = 1;i <= n;i++)
    62             ans = max(ans,dp[1][i]+dp[i+1][n]);
    63         printf("%d
    ",ans);
    64     }
    65     return 0;
    66 }
  • 相关阅读:
    改变Edit的光标(使用CreateCaret,ShowCaret和LoadBitmap三个API函数)
    浅析Delphi Container库(有开源的DCLX)
    Delphi接口的底层实现(接口在内存中仍然有其布局,它依附在对象的内存空间中,有汇编解释)——接口的内存结构图,简单清楚,深刻 good
    Asp.Net在多线程环境下的状态存储问题
    C#程序中注释过多的8条理由
    CentOS 6.4 编译安装LLVM3.3,Clang和Libc++
    Microsoft 2013校园招聘笔试题及解答
    代码契约CodeContract(八)
    T-SQL 临时表、表变量、UNION
    BST&AVL&红黑树简单介绍
  • 原文地址:https://www.cnblogs.com/kuangbin/p/3327691.html
Copyright © 2011-2022 走看看