HackerRank transform-to-palindrome 区间动规

Transform to Palindrome

The Orion alphabet system consists of  letters, denoted by the integers from  to . The  Orion letter is denoted by the integer .

Some Orion letters can be transformed to other Orion letters. A transformation is denoted by a pair of two Orion letters, . Using this transformation, you can replace letter  with letter .

Transformations also have additional properties:

  1. If letter  can be transformed to letter  using a transformation, then letter  can be transformed to letter  as well.
  2. If letter  can be transformed to letter  and letter  can be transformed to letter , then letter  can be transformed to letter  as well.

You are given a sequence  comprising of  Orion letters. You are given  transformations that can be applied to . You may apply transformations to zero or more letters in the sequence. When a transformation is applied to a letter, the other letters of the string remain unaffected. You can also apply a single transformation multiple times on the same sequence.

Print the length of the longest possible palindromic subsequence after applying zero or more transformations on the letters of the given sequence.

For example, in the sequence below, transformation  is first applied to the sequence  to obtain . The longest palindrome subsequence is then obtained from the resulting transformed sequence by removing letter .

Input Format

The first line contains three space separated integers  and . The following  lines each contain two space separated integers  and , denoting a transformation from letter  to letter . The last line contains  positive integers (elements of the string).

Constraints

Output Format

Print a single line containing an integer denoting the length of the longest possible palindromic subsequence which can be obtained after applying transformations on the given string.

Sample Input 0

10 7 6
1 3
5 7
3 5
2 6
2 4
8 4
10 9
1 9 2 3 10 3

Sample Output 0

5

Explanation 0

The given string is . After transforming the last letter from  to , string becomes . After transforming  to , string becomes . One of the longest palindromic subsequence is formed as follows .

Sample Input 1

10 8 15
1 2
1 3
2 7
3 1
4 5
6 8
9 6
10 5
1 4 5 7 9 8 1 3 10 4 5 10 2 7 8

Sample Output 1

10


区间dp,没啥可说的,就是最简单的dp。代码粘不上来,那就不粘了吧。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值