hdu 5583 Kingdom of Black and White 思路题

Kingdom of Black and White
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 1206 Accepted Submission(s): 375

Problem Description

In the Kingdom of Black and White (KBW), there are two kinds of frogs: black frog and white frog.

Now N frogs are standing in a line, some of them are black, the others are white. The total strength of those frogs are calculated by dividing the line into minimum parts, each part should still be continuous, and can only contain one kind of frog. Then the strength is the sum of the squared length for each part.

However, an old, evil witch comes, and tells the frogs that she will change the color of at most one frog and thus the strength of those frogs might change.

The frogs wonder the maximum possible strength after the witch finishes her job.

Input

First line contains an integer T, which indicates the number of test cases.

Every test case only contains a string with length N, including only 0 (representing
a black frog) and 1 (representing a white frog).

⋅ 1≤T≤50.

⋅ for 60% data, 1≤N≤1000.

⋅ for 100% data, 1≤N≤105.

⋅ the string only contains 0 and 1.

Output

For every test case, you should output “Case #x: y”,where x indicates the case number and counts from 1 and y is the answer.

Sample Input

2
000011
0101

Sample Output

Case #1: 26
Case #2: 10

Source

2015ACM/ICPC亚洲区上海站-重现赛(感谢华东理工)

Recommend

题意 : 给你一个只含有 "0" "1" 的字符串,问你最多可以改变一个字符串后,连续相同的字符串的长度的平方的和,最大值为多少.

    样例1 : 000011   
        连续为 0 的长度是 4 ,连续为 1 的长度是 2 ,当前的值是4*4+2*2=20; 
        改变第四个 01 后,连续为 0 的长度是 5 ,连续为 1 的长度是 1 ,当前的值是 5*5+1*1=26;

    样例2 : 0101
        数字连续相同的长度分别为 1 1 1 1 ,当前值为 1*1+1*1+1*1+1*1=4;
        改变第一个 10 ,或者改变第二个 01 , 连续相同的长度就为 3 1,当前值为 3*3+1*1=10.
    思路:一个数组存储连续相同数字的个数,遍历一遍,不断更新改变前后的最大值.

#include<stdio.h>
#include<string>
#include<iostream>
#include<algorithm>
#include<string.h>
#include<math.h>
using namespace std;
char s[100005];//字符串存储;
long long  f[100005];//当前位置相同的字符的个数;
int len;

//预处理;
void previous(int len)
{
    memset(f,0,sizeof(f));
    f[0]=1;
    for(int i=0; i<len-1; i++)
    {
        if(s[i]==s[i+1])
        {
            f[i+1]=f[i]+1;
            f[i]=0;
        }
        else
            f[i+1]++;
    }
    int present=f[len-1];
    for(int i=len-2; i>=0; i--)
    {
        if(!f[i])
            f[i]=present;
        else
            present=f[i];
    }
}

int main()
{
    int T,t=1;//t 用来记录case;
    scanf("%d",&T);
    while(T--)
    {
        scanf("%s",s);
        len=strlen(s);//长度;

        //特判,省点时间;可以不要;
        if(len<4)
        {
            printf("%d\n",len*len);
            continue;
        }

        previous(len);//预处理;

        for(int i=0; i<len; i++)
            printf("%d ",f[i]);
        printf("\n");


        long long  sum=f[0]*f[0];
        for(int i=1; i<len; i++)
            if(s[i]!=s[i-1])
                sum+=f[i]*f[i];

//        printf("sum = %d\n",sum);

        long long   now=0,sum1=sum;
        //sum1 用来记录最大值;
        //sum  用来记录初始值;
        //now  用来记录变化当前位置后的值;

        for(int i=1; i<len-1; i++)
        {
            if(s[i]!=s[i-1])//如果前当前位置不等于前边一位;
            {
                if(s[i]==s[i+1])//如果当前位置等于后一位;
                {
                    int a=max(f[i],f[i-1]);
                    int b=min(f[i],f[i-1]);
                    now=sum+  (a+1)*(a+1)+(b-1)*(b-1)-a*a-b*b;
                }
                else //如果当前位置不等于后一位;
                    now=sum -f[i-1]*f[i-1]-f[i+1]*f[i+1]-1+(1+f[i+1]+f[i-1])*(1+f[i+1]+f[i-1]);
                sum1=max(max(sum,now),sum1);
            }
        }
        if(s[len-1]!=s[len-2])//特判最后一位;
            now=sum+(f[len-2]+1)*(f[len-2]+1)-1-f[len-2]*f[len-2];
        sum1=max(now,sum1);
        printf("Case #%lld: %lld\n",t++,sum1);
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值