2015 number of input sub-sequences in the US qualifying round of programming, 2015
Time Limit: 2000 ms
Single-point time limit: 1000 ms
Memory limit: 256 MB
Description
Returns the number of subsequences of input strings. The reversed Character Sequence is still the same as the original sequence. For example, in the string aba, the input sub-sequence is "a", "a", "aa", "B", and "aba". There are five sub-sequences in total. Subsequences with the same content and different locations calculate different subsequences.
Input
The first line is an integer T, indicating the number of data groups. Followed by T group data, each group of data is a string.
Output
Output a row of data in each group in the format of "Case # X: Y". X indicates the data number (starting from 1) and Y indicates the answer. The answer is modulo 100007.
Data range
1 ≤ T ≤ 30
Small Data
String Length ≤ 25
Big Data
String Length ≤ 1000
Sample Input
5
Aba
Abcbaddabcba
12111112351121
Ccccccc
Fdadfa
Sample output
Case #1: 5
Case #2: 277
Case #3: 1333
Case #4: 127
Case #5: 17
Analysis:
1.For interval-type DP, Let's first look at the state equation: for example, to find the number of input subsequences in the range I to j. ① When s [I] = s [j, for example, ".... A ",D [I] [j] = d [I] [J-1] + d [I + 1] [j] + 1(D [I] [j] = 2 * d [I + 1] [J-1] + d [I] [J-1] + d [I + 1] [j] + 1- 2 * d [I + 1] [J-1]Simplified. Explanation: it is known that I + 1 to J-1 has d [I + 1] [J-1] Input subsequences, and s [I] = s [j], then it can form a d [I + 1] [J-1] echo sub-sequence with the middle (string I + 1 to J-1) known, coupled with the original string contained in the middle of the sub-sequence of 2 * d [I + 1] [J-1], two a combination of "aa" is also the return, so add 1, calculate the sub-sequences of a and intermediate strings on both left and right, respectively, but this time is not complete yet. Note: When calculating the and intermediate strings on both left and right, calculate the substring contained in the intermediate string twice, so subtract 2 * d [I + 1] [J-1]); ② When s [I]! = S [j,D [I] [j] = d [I] [J-1] + d [I + 1] [j]-d [I + 1] [J-1]. This can be analyzed as well as above.
2.D [I + 1] [J-1], d [I] [J-1], d [I] [], d [I + 1] [j] is used for finding the range I to j. This is precisely why the interval-type DP generally starts from a relatively small interval and does not expand the interval. The subintervals used for the current range (I, j) have been completed before. so is the sentence, just use it directly.
Disagree in that place. Please point out and learn from each other !!
# Include <iostream> # include <cstdio> # include <string. h ># include <cstring> using namespace std; const int MOD = 100007; int t, len, d [1010] [1010]; char s [1010]; void dp () {for (int I = 1; I <len; I ++) {for (int j = 0; j <(len-I); j ++) {if (s [j] = s [j + I]) d [j] [I + j] = (d [j] [I + J-1] + d [j + 1] [I + j] + 1) % MOD; else d [j] [I + j] = (d [j] [I + J-1] + d [j + 1] [I + j]-d [j + 1] [I + J-1] + MOD) % MOD ;}}int main () {cin> t; for (int q = 1; q <= t; q ++) {memset (s, 0, sizeof (s); memset (d, 0, sizeof (d); cin> s; len = strlen (s); for (int I = 0; I <len; I ++) d [I] [I] = 1; dp (); printf ("Case # % d: % d \ n", q, d [0] [len-1] % MOD);} return 0 ;}View Code