Noi question Bank/2.6 Dynamic programming of basic algorithms-8471: Cutting palindrome

Source: Internet
Author: User

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&GT;=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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.