Title Link: http://www.lightoj.com/volume_showproblem.php?problem=1025
The main topic: a string of characters, by removing some of the characters, can make this string of characters into a palindrome string. Now give you a string of characters and ask how many different palindrome strings can be obtained;
Note: The string "ABBA", You can get 9 string palindrome, respectively, ' A ', ' a ', ' B ', ' B ', ' AA ', ' BB ', ' aba ', ' ABA ', ' ABBA '.
Problem-solving ideas: declaring Dp[i][j] as a string [i,j] interval by deleting the number of different palindrome strings can be obtained
Then there are two scenarios:
1: When str[i]! = Str[j], dp[i][j] = Dp[i][j-1] + dp[i+1][j]-dp[i+1][j-1]; (The reason for subtracting dp[i+1][j-1] is the previous two items add a dp[i+1][j-1])
2: when str[i] = = Str[j], dp[i][j] = (Dp[i][j-1] + dp[i+1][j]-dp[i+1][j-1]) + (Dp[i+1][j-1] + 1); (the previous item refers to Str[i] and str[j] not be able to group The number of schemes of Cheng strings, and the second is the number of schemes that can form a palindrome when str[i] and str[j] correspond.
Note that it is not possible to First Direct loop I, the second direct loop J, then Dp[i][j], Dp[i+1][j] may not have obtained the correct value.
The DP array code is as follows:
#include <bits/stdc++.h>using namespaceStd;typedefLong Longll;Const intMoD = 1e9 +7;Const intINF =0x3f3f3f3f;Const intN =103; ll Dp[n][n];CharStr[n];voidSolveintcases) {scanf ("%s", str); intL =strlen (str); Memset (DP,0,sizeof(DP)); for(intI=0; Str[i]; ++i) dp[i][i]=1; for(intlen=1; len<l; ++Len) { for(intI=0; i+len<l; ++i) {intj=i+Len; if(Str[i]! =Str[j]) dp[i][j]= dp[i][j-1] + dp[i+1][J]-dp[i+1][j-1]; ElseDp[i][j]= dp[i][j-1] + dp[i+1][J] +1; }} printf ("Case %d:%lld\n", cases, dp[0][l-1]);}intMain () {intT; scanf ("%d", &T); for(intI=1; i<=t; ++i) solve (i); return 0;}
View Code
The memory search code is as follows:
#include <bits/stdc++.h>using namespaceStd;typedefLong Longll;Const intMoD = 1e9 +7;Const intINF =0x3f3f3f3f;Const intN =103; ll Dp[n][n];CharStr[n];ll DFS (intLintR) { if(L = =R)returnDP[L][R] =1; if(Dp[l][r]! =-1) returnDp[l][r]; if(L >R)return 0; ll ans; if(Str[l]! =Str[r]) ans= DFS (l, R-1) + DFS (l +1, R)-DFS (L +1, R-1); Elseans= DFS (l, R-1) + DFS (l +1, R) +1; returnDP[L][R] =ans;}voidSolveintcases) {scanf ("%s", str); intL =strlen (str); Memset (DP,-1,sizeof(DP)); printf ("Case %d:%lld\n", Cases, DFS (0, L-1));}intMain () {intT; scanf ("%d", &T); for(intI=1; i<=t; ++i) solve (i); return 0;}
View Code
Light OJ 1025-the specials Menu (Dynamic planning-interval DP)