16-Minimum Palindrome array

Source: Internet
Author: User

Splits the string time limit:MS | Memory limit:65535 KB Difficulty:3
Describe
HRDV is interested in a string,especially the palindrome string. So he wants some palindrome string.
A sequence of characters is a palindrome if it is the same written forwards and backwards. For example, ' Abeba ' was a palindrome, but ' ABCD ' was not.
A partition of sequence of characters are a list of one or more disjoint non-empty groups of consecutive characters whose Concatenation yields the initial sequence. For example, (' Race ', ' car ') was a partition of ' racecar ' into the groups.
Given a sequence of characters, we can always create a partition of these characters such this each group in the partition is a palindrome! Given This observation it's natural to ask:what are the minimum number of groups needed for a Given string such that ever Y group is a palindrome?
For example:
' Racecar ' is already a palindrome, therefore it can being partitioned into one group. ' Fastcar ' does not contain any non-trivial palindromes, so it must is partitioned as (' F ', ' a ', ' s ', ' t ', ' C ', ' A ', ' R '). ' AAADBCCB ' can be partitioned as (' AAA ', ' d ', ' BCCB '). Input begins with the number N of test cases. Each test case consists of a single line of between 1 and lowercase letters, with no whitespace within.
Input
Each test case consists of a single line of between 1 and lowercase letters, with no whitespace within.
Output
For each test case, output a line containing the minimum number of groups required to partition the input into groups of P Alindromes.
Sample input
Racecarfastcaraaadbccb
Sample output
173
Uploaded by
tc_ Hu Jendong
#include <iostream>
#include <cstring>
#include <cstdio>
using namespace Std;
int dp[1005];
Char a[1005];

int judge (int i, int j) {

while (a[i] = = A[j]) {
if (i = = j)
Break
i++;
if (i = = j)
Break
j--;
if (i = = j)
Break
//    }
if (i = = J && a[i] = = A[j])
return 1;
Else
return 0;
It's too much trouble judging the palindrome.
int mid = (i + j) >> 1;
int m = i;
for (int i; I <= mid; i++) {
if (a[i]! = a[j-i + M])//Note j-i When you add the starting point of the array fragment.
return 0;
}
return 1;
}

int main () {
while (~SCANF ("%s", A + 1)) {
int len = strlen (A + 1);
for (int i = 1; I <= len; i++) {
Dp[i] = i;
for (int j = 1; J <= I; j + +) {
if (a[i] = = A[j] && judge (J, I)) {//dp[i] represents the minimum number of palindrome numbers from 0 to I
Dp[i] = min (Dp[i], dp[j-1] + 1); J from is starting from 0 scanning to I if found J to I is a palindrome,
Then j to I is a palindrome, you can get this recursive relationship
}
}
}
printf ("%d\n", Dp[len]);
}
return 0;
}

16-Minimum Palindrome array

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.