UVA 10617 Again palindrome (DP)

Source: Internet
Author: User

UVA 10617 Again palindrome The main idea: give a string, you can delete the operation, you can delete any number of arbitrary position (can be 0) characters. Ask, the delete operation makes the original string into a palindrome string, there are several ways. Solution: Dp[i][j] = 1 (i = = j), a single character is also palindrome string s[i]! = S[j], dp[i][j] = dp[i + 1][j] + dp[i][j-1]-dp[i + 1][j-1],dp[i + 1] [j] and DP[I][J + 1] public part Dp[i + 1][j-1] to lose one s[i] = = S[j], dp[i][j] = dp[i + 1][j] + dp[i][j-1] + 1, when s[i] = = S[j], than S [i]! = S[j] More A class of cases, that is dp[i+1][j-1] any palindrome substring can be added at the same time s[i] and s[j], and S[i]s[j] is also a palindrome string
#include <cstdio>#include <cstring>#include <algorithm>#include <cmath>#include <cstdlib>using namespace STD;typedef Long LongllChars[ -];ll dp[ -][ -];intMain () {intTscanf("%d", &t); while(t--) {scanf('%s ', s);intLen =strlen(s); for(inti =0; i < Len; i++) {Dp[i][i] =1; } for(inti = len-2; I >=0; i--) { for(intj = i +1; J < Len; J + +) {if(S[i] = = S[j]) {Dp[i][j] = dp[i +1][J] + dp[i][j-1] +1; }Else{Dp[i][j] = dp[i +1][J] + dp[i][j-1]-Dp[i +1][j-1]; }            }        }printf("%lld\n", dp[0][len-1]); }return 0;}

UVA 10617 Again palindrome (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.