BZOJ3790: Magic Necklace

Source: Internet
Author: User

3790: Magic Necklace Time limit:10 Sec Memory limit:64 MB
submit:79 solved:40
[Submit] [Status] Description Mother's Day is coming, little H ready to give her a special necklace. This necklace can be seen as a string of lowercase letters, each lowercase letter representing a color. To make this necklace, little H bought two machines. The first machine can generate all forms of palindrome string, the second machine can connect two palindrome strings, and the second machine has a special property: If a string suffix and a string prefix is exactly the same, then you can overlap this repeating part. For example: ABA and ACA are connected and can generate string Abaaca or abaca. Now give the style of the target necklace and ask how many times you need to use the second machine to generate this special necklace. Input data has multiple lines, one string per line, representing the style of the target necklace. Output multiple lines, one answer per line indicates the minimum number of times a second machine needs to be used. Sample InputABCDCBA
Abacada
ABCdefSample Output0
2
5Hint each test data, enter no more than 5 lines

The string length of each line is less than or equal to 50000: I think hehe ...
I think so, let's manacher first, make f[i] show the minimum cost of playing 1--i
Then O (n) can be used to find the longest palindrome string ending at each point, and it may be possible to extend the farthest to X,
Then you can use min (F[j]) (x-1<=j<=i-1) + to update f[i]
You can have a line tree.
Code:
1#include <cstdio>2 3#include <cstdlib>4 5#include <cmath>6 7#include <cstring>8 9#include <algorithm>Ten  One#include <iostream> A  -#include <vector> -  the#include <map> -  -#include <Set> -  +#include <queue> -  +#include <string> A  at #defineINF 1000000000 -  - #defineMAXN 500000+5 -  - #defineMAXM 20000000+5 -  in #defineEPS 1e-10 -  to #definell Long Long +  - #definePA pair<int,int> the  * #defineFor0 (i,n) for (int i=0;i<= (n); i++) $ Panax Notoginseng #defineFor1 (i,n) for (int i=1;i<= (n); i++) -  the #defineFor2 (i,x,y) for (int i= (x); i<= (y); i++) +  A #defineFor3 (i,x,y) for (int i= (x); i>= (y); i--) the  + #defineMoD 1000000007 -  $ using namespacestd; $  -InlineintRead () -  the { - Wuyi     intx=0, f=1;CharCh=GetChar (); the  -      while(ch<'0'|| Ch>'9'){if(ch=='-') f=-1; ch=GetChar ();} Wu  -      while(ch>='0'&&ch<='9') {x=Ten*x+ch-'0'; ch=GetChar ();} About  $     returnx*F; -  - } - intN,M,A[MAXN],F[MAXN],P[MAXN]; A CharS[MAXN]; + structseg{intK,l,r,mi;} t[4*MAXN]; theInlinevoidBuildintKintLintR) - { $T[k].l=l;t[k].r=r;intMid= (l+r) >>1; t[k].mi=inf; the     if(L==R) {if(!l) t[k].mi=-1;return;} theBuild (k<<1, l,mid); Build (k<<1|1, mid+1, R); the } theInlinevoidPushup (intk) - { inT[k].mi=min (t[k<<1].mi,t[k<<1|1].MI); the } theInlinevoidChangeintKintXinty) About { the     intL=t[k].l,r=t[k].r,mid= (l+r) >>1; the     if(L==R) {t[k].mi=min (t[k].mi,y);return;} the     if(x<=mid) Change (k<<1, x, y);ElseChange (k<<1|1, x, y); + Pushup (k); - } theInlineintQueryintKintXinty)Bayi { the     intL=t[k].l,r=t[k].r,mid= (l+r) >>1; the     if(l==x&&r==y)returnT[K].MI; -     if(Y<=mid)returnQuery (k<<1, x, y); -     Else if(X>mid)returnQuery (k<<1|1, x, y); the     Else returnMin (Query (k<<1, X,mid), query (k<<1|1, mid+1, y)); the } the  the intMain () -  the { the  theFreopen ("Input.txt","R", stdin);94  theFreopen ("output.txt","W", stdout); the  the      while(SCANF ("%s", s)! =EOF)98     { Aboutn=strlen (s); -Build1,0, n);101For1 (i,n) a[i<<1]=s[i-1];102m=n;n=2*n+1;103         intId=0, mx=0;104 For1 (i,n) the         {106             if(mx>i) P[i]=min (p[2*id-i],mx-i);107              while(i-p[i]-1>0&&i+p[i]+1<=n&&a[i-p[i]-1]==a[i+p[i]+1]) p[i]++;108             if(I+P[I]&GT;MX) mx=i+p[i],id=i;109         } the         intnow=0;111For1 (I,n)if(i+p[i]>Now ) the         {113For2 (j,now+1, I+p[i]) f[j]=2*i-J; thenow=i+P[i]; the         } theFor1 (i,m) Change (1, I,query (1, Max (0, (f[i<<1]>>1)-1), I-1)+1);117printf"%d\n", Query (1, M,m));118Memset (A,0,sizeof(a));119memset (s),0,sizeof(s)); -Memset (P,0,sizeof(P));121Memset (F,0,sizeof(f));122     }123 124     return 0; the 126}
View Code

Just started to install force with get results wa a few hair feeling life hopeless, this problem is a not, on the decadent a lesson t_t

The Upd:orzky of the http://blog.csdn.net/iamzky/article/details/41852799

BZOJ3790: Magic Necklace

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.