Uva-11584-partitioning by Palindromes

Source: Internet
Author: User

First use Manacher to find all palindrome string, and then violence statistics with each character as the end of all palindrome string, and then DP is easy. Worst case is O (n^2)

1#include <cstdio>2#include <cstring>3#include <iostream>4#include <algorithm>5#include <vector>6 #definePB Push_back7 using namespacestd;8 Const intmaxn=1010;9 CharS[MAXN];Ten intLen; One intDP[MAXN]; Avector<int>D[MAXN]; - Charma[maxn*2]; - intmp[maxn*2]; the voidManacher () - { -     intL=0; -ma[l++]='$'; +ma[l++]='#'; -      for(intI=0; i<len;i++) +     { Ama[l++]=S[i]; atma[l++]='#'; -     } -ma[l]=0; -     intmx=0, id=0; -      for(intI=0; i<l;i++) -     { inMp[i]=mx>i?min (mp[2*id-i],mx-i):1; -          while(Ma[i+mp[i]]==ma[i-mp[i]]) mp[i]++; to         if(i+mp[i]>mx) +         { -mx=i+Mp[i]; theId=i; *         } $     }Panax Notoginseng } - voidgetd () the { +      for(intI=2; I<= (len<<1); i++) A          for(intj=i;j<i+mp[i];j++) the             if((j&1)==0) d[j/2-1].PB ((2*I-J)/2-1); + } - intMain () $ { $     intT; -scanf"%d",&T); -      for(intKase=1; kase<=t;++Kase) the     { -Memset (MP,0,sizeof(MP));WuyiMemset (DP,0x3f,sizeof(DP)); thescanf"%s", s); -len=strlen (s); Wu Manacher (); - getd (); About          for(intI=0; i<len;i++) $         { -              for(intj=0; J<d[i].size (); j + +) -Dp[i]=min (Dp[i], (d[i][j]==0?1:d p[d[i][j]-1]+1)); - d[i].clear (); A         } +printf"%d\n", dp[len-1]); the     } -}

Then see LRJ code, Memory search, some violence. Before I heard that the VIS does not have to be initialized every time, this is the first time to see how to write.

int Kase; {    ....     if return p[i][j];     = Kase;    ....} int Main () {    ...     .. 0 sizeof (Vis));      for 1; Kase <= T; kase++)    {    ...    }    .....}

Uva-11584-partitioning by Palindromes

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.