Title Link: http://acm.hdu.edu.cn/showproblem.php?pid=5583
Kingdom of Black and white
Time limit:2000/1000 MS (java/others)
Memory limit:65536/65536 K (java/others)
Problem DescriptionIn the Kingdom of Black and White (KBW), there is the kinds of frogs:black frog and white frog.
NowN Frogs is standing in a line, some of them is black, the others is white. The total strength of those frogs is calculated by dividing the line to minimum parts, each part should still is Contin Uous, and can only contain one kind of frog. Then the strength are the sum of the squared length for each part.
However, an old, evil witch comes, and tells the frogs that she'll change the color of
the in most one frog and Thu s the strength of those frogs might change.
The Frogs wonder the
maximum possible strength after the witch finishes her job.
Inputfirst line contains an integerT, which indicates the number of test cases.
Every test case only contains a string with lengthN, including only0(representing
A black frog) and1(representing a white frog).
-1≤T≤ .
-For 60% data,1≤N≤ .
-For 100% data,1≤N≤5.
The string is only contains 0 and 1.
Outputfor Every test case, you should output "
Case #x: y", where x Indicates the case number and counts from 1 and y are the answer.
Sample Input20000110101
Sample outputcase #1:26Case #2:10 Test Instructions: Change the color of a fox, let the strength reach the maximum; thought: Because it is the sum of squares, so the greater the number of the final value, so the next two of the number is relatively small-1, a large +1, Equal to 1 when the special sentence, see the code for details; AC Code:
1#include <iostream>2#include <cstdio>3#include <cstring>4#include <algorithm>5 using namespacestd;6 Const intn=1e5+4;7 Long LongDp[n];8 CharStr[n];9 intMain ()Ten { One intT; Ascanf"%d",&t); - intCnt=1; - while(t--) the { - intnum=1; - Long Longans=0, sum=0; -scanf"%s", str); + intlen=strlen (str); -Memset (DP,0,sizeof(DP)); +dp[1]=1; A for(intI=1; i<len;i++) at { - if(str[i]==str[i-1]) - { -dp[num]++; - } - Else in { -sum+=dp[num]*Dp[num]; tonum++; +dp[num]++; - } the } *sum+=dp[num]*Dp[num]; $ if(num==1)Panax Notoginseng { -ans=dp[1]*dp[1]; the } + for(intI=2; i<=num;i++) A { the if(dp[i]==1) + { -Ans=max (ans,sum+2* (dp[i-1]*dp[i+1]+dp[i-1]+dp[i+1])); $ } $ Else - { - if(dp[i-1]>=Dp[i]) the { -Ans=max (ans,sum+2* (dp[i-1]-dp[i]+1));Wuyi } the ElseAns=max (ans,sum+2* (dp[i]-dp[i-1]+1)); - } Wu } -cout<<"Case #"<<cnt++<<": "<<ans<<"\ n"; About } $ return 0; -}
hdu-5583 Kingdom of Black and white (maths, greed, violence)