D - Wide Flip
Time limit : 2sec / Memory limit : 256MB
Score : 500 points
Problem Statement
You are given a string S consisting
of 0 and 1.
Find the maximum integer K not greater than |S| such
that we can turn all the characters of S into 0 by
repeating the following operation some number of times.
- Choose a contiguous segment [l,r] in S whose
length is at least K (that is, r−l+1≥K must
be satisfied). For each integer i such that l≤i≤r,
do the following: if Si is
0, replace it with1; if Si is1, replace it with0.
Constraints
- 1≤|S|≤105
- Si(1≤i≤N) is
either
0or1.
Input
Input is given from Standard Input in the following format:
S
Output
Print the maximum integer K such
that we can turn all the characters of S into 0 by
repeating the operation some number of times.
Sample Input 1
010
Sample Output 1
2
We can turn all the characters of S into 0 by
the following operations:
- Perform the operation on the segment S[1,3] with
length 3. S is
now
101. - Perform the operation on the segment S[1,2] with
length 2. S is
now
011. - Perform the operation on the segment S[2,3] with
length 2. S is
now
000.
找出最大的k,满足题意。哈哈哈哈哈,实在不想翻译,偷懒一波
POINT:
如果k是x,
那么比如11101000,从左开始数,第k+1开始到末尾都可以任意变0,1。 比如变个[1,k]和[1,k+1],那么k+1这个位置就变了。
从右开始数,那么[1,len-k]都可以任意变0,1。所以我们只要遍历x,然后找到他们从左从右都不能任意变的重合位置,判断这些位置上的值是不是相同的,若相同
则这个x可以,不相同就绝对不能成功。
感觉做的有点蠢。
#include <set>
#include <map>
#include <math.h>
#include <vector>
#include <stdio.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
#define LL long long
const double pi = acos(-1);
const double g = -9.8;
const int maxn = 1e5+555;
int main()
{
char s[maxn];
scanf("%s",s);
int ll=strlen(s);
int ans=ll/2;
int l,r;
while(1){
l=ll-ans;
r=ans+1;
int flag=1;
int now=s[l-1];
for(int i=l;i<r;i++){
if(s[i]!=now){
flag=0;
break;
}
}
if(!flag){
break;
}
ans++;
}
printf("%d\n",ans);
}

本文介绍了一个有趣的编程挑战,目标是在给定字符串S由0和1组成的情况下,寻找最大整数K,使得通过一系列操作可以使所有字符变为0。文章详细解释了解决方案的思路,并提供了完整的代码实现。
341

被折叠的 条评论
为什么被折叠?



