https://blog.csdn.net/is_cp/article/details/41511269
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
题目分析:
根据题意的话,n块石头围一圈。一只兔子顺时针,一只兔子逆时针(限制在一圈的范围内)。如果把数组扩大一倍,再求[i,i+n]之间的最长回文就行了。为什么要这么做呢,[i,i+n]可以满足从任一点开始顺时针走一圈。为什么要求回文呢? 假如存在回文序列,第一个兔子在a点,回文序列里与a对应的点是b点。那么第二个兔子就可以从b点按相反的方向走。最终走到第一个兔子开始的地方。这个过程里兔子前半段跟后半段走的路是一样的。
http://www.cnblogs.com/chen9510/p/5860395.html
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
题目分析:
根据题意的话,n块石头围一圈。一只兔子顺时针,一只兔子逆时针(限制在一圈的范围内)。如果把数组扩大一倍,再求[i,i+n]之间的最长回文就行了。为什么要这么做呢,[i,i+n]可以满足从任一点开始顺时针走一圈。为什么要求回文呢? 假如存在回文序列,第一个兔子在a点,回文序列里与a对应的点是b点。那么第二个兔子就可以从b点按相反的方向走。最终走到第一个兔子开始的地方。这个过程里兔子前半段跟后半段走的路是一样的。
http://www.cnblogs.com/chen9510/p/5860395.html
题意:两只兔子,在n块围成一个环形的石头上跳跃,每块石头有一个权值ai,一只从左往右跳,一只从右往左跳,每跳一次,两只兔子所在的石头的权值都要相等,在一圈内(各自不能超过各自的起点,也不能再次回到起点)它们最多能经过多少个石头(1 <= n <= 1000, 1 <= ai <= 1000)。
分析:其实就是求一个环中,非连续最长回文子序列的长度。
但是,这个dp公式仅仅是求出一个序列的非连续最长回文子序列,题目的序列是环状的,有两种思路:
dp[i][j] = max{ dp[i + 1][j], d[i][j - 1], (a[i]= =a[j])*dp[i + 1][j - 1] + 2 }
但是,这个dp公式仅仅是求出一个序列的非连续最长回文子序列,题目的序列是环状的,有两种思路:
- 将环倍增成链,求出窗口为n的最长子序列,但这不是最终的解,你可以试看看Sample 2,是否能得出4,因为它在选中的回文外面还可以选中一个当做起点来跳,所以外面得判断找出来的回文外面是否还有可以当起点的石头,即可以找窗口为(n-1)的长度+1。所以解即找 窗口为n的长度或者 窗口为(n-1)的长度+1 的最大值。什么意思呢?第二组样例:1 2 1 1 最长回文子序列长为3,序列为1 2 1,但可以再找一个共同的起点1,序列为(1)1 2 1 1,所以长为4;
- 不倍增,直接当成一个链求dp,然后把链切成两半,求出两边的回文长度,最大的和就是解。这里不用考虑起点问题,因为两边的回文中点都可以做起点。
while(scanf("%d",&n)!=EOF&&n) { for(int i=1;i<=n;i++) { scanf("%d",&a[i]); a[i+n]=a[i]; } memset(dp,0,sizeof(dp)); for(int i=1;i<=2*n;i++) dp[i][i]=1; for(int len=1;len<n;len++) { for(int i=1;i+len<=2*n;i++) { dp[i][i+len]=max(dp[i][i+len-1],max(dp[i+1][i+len],dp[i+1][i+len-1]+(a[i]==a[i+len])*2)); } } int tmp=0; for(int i=1;i<=n;i++) tmp=max(tmp,dp[i][i+n-1]); for(int i=1;i<=n;i++) tmp=max(tmp,dp[i][i+n-2]+1); cout<<tmp<<endl; }
此题要求的就是最长回文子序列(并不是子串),而最长回文子序列的算法复杂度为O(n*n)。但是由于是个环上,所以要挖掘一下环的性质。
假设两只兔子跳的是序列,而不是环,那么他们是这样跳的4<-3<-2<-1->2->3->4,一共跳了4次,颜色的不同代表了不同的兔子,那么现在将这个序列接起来,就是4321234的环了,在环上他们跳的次数是7次。可以发现其中一只兔子跳了1->2->3->4->4->3->2,而另一只是逆着跳而已。观察一下,其实算2->3->4就行了,下一段肯定是一个逆的过程(另一兔子所跳的)。那么就只需要算一个序列的最长回文子序列啦。注意起点必须是两只一块站着,所以这种情况下必定是个奇数。
如果序列并不是这样的呢?比如12213443,在序列中的某一处断开了,变成两段回文,由于是个环,所以是两段的最长回文子序列之和。枚举一下断开处就行了,注意只需要一个起点,即只要有一段是个奇数就行了。
注:这道题这样提交的话是WA的,但是代码是没有错的。而起点算两次的代码却可以AC。
思路:放了一个环上,如果从起点反向之后,每次跳的位置的数字相同,我们把他们的相遇位置切开,可以发现是一个回文串。
所以问题就是一个环求最长的回文串了。按照平常的思路,倍增这个串,然后正常求就可以了。
有一个问题就是对于偶数长度的时候,如第二组样例,我们可以把他看成n-1的串,最终跳到了1上。