HDU-5583-Kingdom of Black and White(预处理+枚举)

Kingdom of Black and White

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

解题思路:

写吐了,数据还爆int。
先预处理出对于每个位置,左边有多少个连续的0和1,以及右边有多少个连续的0和1。
先遍历一遍计算出原始串的答案。
然后枚举每个位置上的颜色都更改一次,然后计算更改后相对于更改前的变化量。这里有很多种情况如 101 110 011 111 001 100。
最后保存一个最大的变化量加上原始串的答案,就是答案。

AC代码:

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <vector>
#include <queue>
#include <map>
#include <algorithm>
#define int long long
using namespace std;

const int mod = 1e9+7;
const int N = 1e6+10;
int back_zero[N];
int front_zero[N];
int back_one[N];
int front_one[N];

int mypow(int x)
{
    return x*x;
}

signed main()
{
    int t;
    int cas = 1;
    cin>>t;
    while(t--)
    {
        string s;
        cin>>s;
        s = "#"+s+"#";
        int len = s.size();
        back_zero[len-1] = back_one[len-1] =  0;
        for(int i = len - 2 ; i > 0 ;i  --)
        {
            if(s[i] == '0')
            {
                back_one[i] = 0;
                back_zero[i] = back_zero[i+1]+1;
            }
            else
            {
                back_zero[i] = 0;
                back_one[i] = back_one[i+1]+1;
            }
        }
        front_zero[0] = front_one[0] =  0;
        for(int i = 1; i < len-1 ;i  ++)
        {
            if(s[i] == '0')
            {
                front_one[i] = 0;
                front_zero[i] = front_zero[i-1]+1;
            }
            else
            {
                front_zero[i] = 0;
                front_one[i] = front_one[i-1]+1;
            }
        }
        int ans = 0;
        for(int i = 1 ; i < len-1 ; i  ++)
        {
            char tmp = s[i];
            int tmp1 = 0;
            while(i < len-1 && s[i] == tmp){
                    tmp1++;
                    i++;
            }
            ans += mypow(tmp1);
            i--;
        }
        int max1 = 0;
        for(int i = 1 ; i < len-1;i++)
        {
            int tmp = 0;
            if(s[i] == '0')
            {
                if(front_one[i-1] && back_one[i+1])
                {
                    tmp -= mypow(front_one[i-1])+mypow(back_one[i+1]) + 1;
                    tmp += mypow(front_one[i-1] + back_one[i+1] + 1);
                }
                else if(front_one[i-1] && back_zero[i+1])
                {
                    tmp += mypow(front_one[i-1]+1);
                    tmp -= mypow(front_one[i-1]);
                    tmp -= mypow(back_zero[i+1]+1);
                    tmp += mypow(back_zero[i+1]);
                }
                else if(front_zero[i-1] && back_one[i+1])
                {
                    tmp -= mypow(front_zero[i-1]+1);
                    tmp += mypow(front_zero[i-1]);
                    tmp += mypow(back_one[i+1]+1);
                    tmp -= mypow(back_one[i+1]);
                }
                else if(back_one[i+1])
                {
                    tmp += mypow(back_one[i+1]+1);
                    tmp -= mypow(back_one[i+1])+1;
                }
                else if(front_one[i-1])
                {
                    tmp += mypow(front_one[i-1]+1);
                    tmp -= mypow(front_one[i-1])+1;
                }
            }
            else
            {
                if(front_zero[i-1] && back_zero[i+1])
                {
                    tmp -= mypow(front_zero[i-1])+mypow(back_zero[i+1]) + 1;
                    tmp += mypow(front_zero[i-1] + back_zero[i+1] + 1);
                }
                else if(front_one[i-1] && back_zero[i+1])
                {
                    tmp -= mypow(front_one[i-1]+1);
                    tmp += mypow(front_one[i-1]);
                    tmp += mypow(back_zero[i+1]+1);
                    tmp -= mypow(back_zero[i+1]);
                }
                else if(front_zero[i-1] && back_one[i+1])
                {
                    tmp += mypow(front_zero[i-1]+1);
                    tmp -= mypow(front_zero[i-1]);
                    tmp -= mypow(back_one[i+1]+1);
                    tmp += mypow(back_one[i+1]);
                }
                else if(back_zero[i+1])
                {
                    tmp += mypow(back_zero[i+1]+1);
                    tmp -= mypow(back_zero[i+1])+1;
                }
                else if(front_zero[i-1])
                {
                    tmp += mypow(front_zero[i-1]+1);
                    tmp -= mypow(front_zero[i-1])+1;
                }
            }
            max1 = max(max1,tmp);
        }
        ans += max1;
        cout<<"Case #"<<cas++<<": ";
        cout<<ans<<endl;
    }
}

发布了104 篇原创文章 · 获赞 7 · 访问量 4096

猜你喜欢

转载自blog.csdn.net/qq_43461168/article/details/103151482