Ultraviolet A 11584-partitioning by palindromes (simple DP)

Source: Internet
Author: User

Click Open Link

Question:

To a string, you must split it into several substrings so that each substring is a return string. The minimum number of splits.

Analysis:

F [I] indicates the minimum number of strings ending with I that can be divided.

F [I] = min {f [J] + 1, string [J, I] is a return string & 1 <= j <= I}

Code:

# Include <iostream> # include <cstdio> # include <cstring> # include <cmath> # include <algorithm> # include <vector> using namespace STD; typedef long int64; const int INF = 0x3f3f3f; const int maxn = 1010; char STR [maxn]; int f [maxn]; bool ispalind (int l, int R) {While (L <R) {If (STR [l]! = STR [R]) return false; ++ L; -- r;} return true;} int main () {int t; scanf ("% d", & T ); while (t --) {scanf ("% s", STR + 1); int Len = strlen (STR + 1); memset (F, 0, sizeof (f )); for (INT I = 1; I <= Len; ++ I) {f [I] = I + 1; for (Int J = 1; j <= I; + + J) if (ispalind (J, I) {f [I] = min (F [I], F [J-1] + 1 );}} printf ("% d \ n", F [Len]);} return 0 ;}

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.