-
Total time limit:
-
1000ms
-
Memory Limit:
-
65536kB
-
-
Describe
-
-
Ford recently had a very strong interest in palindrome string.
If a string looks exactly the same from left to right and from right to left, then it is considered a palindrome string. For example, "ABCAACBA" is a palindrome string, "Abcaaba" is not a palindrome string.
Ford now obsessive-compulsive disorder, see what string all want to turn it into a palindrome. Fu can be cut through the string, so that the resulting substring after cutting is a palindrome.
Now, Ford wants to know how many times he cuts at least to achieve his goal. For example, for the string "Abaacca", cut at least once, you can get "ABA" and "ACCA" the two palindrome strings.
-
-
Input
-
-
the first line of input is an integer t (T <= 20), representing a total of T-group data.
In the next T-line, each row contains a string of 1000 not exceeding the length, and the string contains only lowercase letters.
-
-
Output
-
-
for each set of data, output one line. The line contains an integer that indicates the minimum number of times the cut is made, so that the resulting substring is a palindrome.
-
-
Sample input
-
-
3abaaccaabcdabcba
-
-
Sample output
-
-
130
-
-
Tips
-
-
for the first set of samples, Ford cut at least 1 times, the original string was cut to "ABA" and "ACCA" two palindrome substring.
For the second set of samples, Ford cut at least 3 times, the original string is cut to "a", "B", "C", "D" the four palindrome substring.
For the third group of samples, Ah Fu does not need to cut, the original string itself is a palindrome string.
-
-
#include <cstring>#include<cstdio>#defineN 1005intT,len,dp[n];CharStr[n];BOOLJudgeintLintR) { for(; str[l]==str[r]; ++l,--R); if(L>=R)return true; Else return false;}intMinintAintb) {returnA>b?b:a;}voidDP (intLintR) { for(intI=l; i<=r; ++i) dp[i]=0x3f3f3f3f; dp[0]=1; for(intI=l; i<=r; ++i) for(intj=0; j<=i; ++j) {if(judge (J,i)) {if(j==0) dp[i]=1; ElseDp[i]=min (dp[i],dp[j-1]+1); } }}intMainintargcChar*argv[]) {scanf ("%d",&T); while(t--) {scanf ("%s", str); Len=strlen (str); DP (0, Len); printf ("%d\n", dp[len-1]-1); } return 0;}
Noi question Bank/2.6 Dynamic programming of basic algorithms-8471: Cutting palindrome