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