Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
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
题目链接:HDU-5583
题目大意:给出一串只含有01的字符串,例如:
最多只能改变一个数字,0->1或者1 -> 0,使得最后平方和最大。
题目思路:直接暴力,枚举一个块边缘两个数字被改变之后的情况,找出最大值
以下是代码:
#include <vector>
#include <map>
#include <set>
#include <algorithm>
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstdlib>
#include <string>
#include <cstring>
using namespace std;
int main(){
int T;
cin >> T;
int cas = 1;
while(T--)
{
string s;
cin >> s;
long long cnt = 1,ans = 0;
char last = s[0];
vector <long long> block;
block.push_back(0);
for (int i = 1; i < s.size(); i++)
{
if (s[i] == last) cnt++;
else
{
ans += cnt * cnt;
block.push_back(cnt);
cnt = 1;
}
last = s[i];
}
block.push_back(cnt);
ans += cnt * cnt;
block.push_back(0);
long long loy = ans;
for (int i = 1; i < block.size() - 2; i++)
{
if (block[i] == 1)
{
long long temp = (block[i + 1] + block[i - 1] + 1) * (block[i + 1] + block[i - 1] + 1) - 1 - block[i + 1] * block[i + 1] - block[i - 1] * block[i - 1];
loy = max(loy,ans + temp);
}
else
{
long long temp2 = (block[i + 1] - 1) * (block[i + 1] - 1) + (block[i] + 1) * (block[i] + 1) - block[i + 1] * block[i + 1] - block[i] * block[i];
loy = max(loy,ans + temp2);
}
if (block[i + 1] == 1)
{
long long temp3 = (block[i] + block[i + 2] + 1) * (block[i] + block[i + 2] + 1) - 1 - block[i] * block[i] - block[i + 2] * block[i + 2];
loy = max(loy,ans + temp3);
}
else
{
long long temp4 = (block[i] + 1) * (block[i] + 1) + (block[i + 1] - 1) * (block[i + 1] - 1) - block[i] * block[i] - block[i + 1] * block[i + 1];
loy = max(loy,ans + temp4);
}
}
printf("Case #%d: %lld\n",cas++,loy);
}
return 0;
}