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)