HDU 4632 palindrome subsequence (range DP, return string, Character Processing)

Source: Internet
Author: User

Question

 

 

Reference self blog: http://blog.csdn.net/u011498819/article/details/38356675

 

 

Question: Find the number of such sub-return strings (not necessarily consecutive, but there is a sequence from left to right.

The simple interval DP, ah, thought it was amazing. In fact, it was also DP, but the parameter was changed to the interval. I did not do this type of question. I could not think of DP.

Now, if you already know [0, I] and push [0, I + 1], you need to go back from I + 1. The DP equation is also simple:
DP [J] [I] = (DP [J + 1] [I] + dp [J] [I-1] + 10007-dp [J + 1] [I-1]) % 10007; minus the repeating
If (s [I] = s [J]) DP [J] [I] = (DP [J] [I] + dp [J + 1] [I-1] + 1) % 10007; Do not forget here, new and ending forms.

 

 

# Include <stdio. h> # include <string. h >#include <algorithm> # include <string> # include <iostream> using namespace STD; int DP [1010] [1010]; // DP [I] [J] I ~ Number of input strings between J: int main () {int t; scanf ("% d", & T); For (INT id = 1; id <= T; id ++) {char s [1010]; scanf ("% s", S); int Len = strlen (s); memset (DP, 0, sizeof (DP); For (INT I = 0; I <Len; I ++) DP [I] [I] = 1; for (INT I = 0; I <Len; I ++) {for (Int J = I-1; j> = 0; j --) {// Add a modulo when there is a subtraction! Otherwise, the number is negative! DP [J] [I] = (DP [J + 1] [I] + dp [J] [I-1]-DP [J + 1] [I-1] + 10007) % 10007; // The model is missing... If (s [I] = s [J]) DP [J] [I] = (1 + dp [J + 1] [I-1] + dp [J] [I]) % 10007; // forget the modulo... } Printf ("case % d: % d \ n", ID, DP [0] [len-1] % 10007);} return 0 ;}
View code

 

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.