Uva 11584, divided into palindrome string

Source: Internet
Author: User

Title Link: https://uva.onlinejudge.org/external/115/11584.pdf

Test instructions

A string, dividing it, so that each string is a palindrome string, the minimum number of palindrome string.

Analysis:

D (i) to the first character of the optimal solution (that is, at least divided into several palindrome string), there is the equation D (i) = min (d (j)) + 1; (where S[j+1,i] is a palindrome).

In this way, the enumeration is O (n^2) complexity, if according to ordinary judgment S[j+1,i] is a palindrome string, time complexity is O (n^3), first with O (n^2) of the complexity of preprocessing is_huiwen[i][j] to determine whether a palindrome string. The DP scheme I used earlier. Here's a better way to write--the center extension.

Note Here is: reserve a dp[0] = 0, so that there is no palindrome string in the current face, and the entire string is a palindrome string, there is d[i] = d[0]+1 = 1;

#include <bits/stdc++.h>using namespacestd;Const intMAXN = ++5;intN,KASE,P[MAXN][MAXN],D[MAXN];BOOLVIS[MAXN][MAXN];CharS[MAXN];intIs_palindrome (intIintj) {    if(I >= J)return 1; if(S[i]! = S[j])return 0; if(Vis[i][j] = =true)returnP[i][j]; VIS[I][J]=true; P[I][J]= Is_palindrome (i+1, J-1); returnp[i][j];}intMain () {intT; scanf ("%d",&T);  for(Kase =1; kase<=t; kase++) {memset (Vis,0,sizeof(VIS)); scanf ("%s", s+1); N= strlen (s+1); d[0] =0; Is_palindrome (1, N);  for(intI=1; i<=n; i++) {D[i]= i+1;  for(intj=0; j<i; J + +)            {                if(Is_palindrome (j+1, i)) D[i]= Min (d[i],d[j]+1); }} printf ("%d\n", D[n]); }    return 0;}

Uva 11584, divided into palindrome string

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.