终于AC了一道(wei)( suo)的水题
题目本身不难,只是很坑。真的很坑......
这里题目大意解释一下:就是有一个项链,在项链的某处咔擦,然后得到一条直线
然后从两端开始取,遇到不同颜色的就停止取。当然,这个题目中还有一个神奇的地方,让整个题目不仅难了不少,也坑了不少。这个神奇的地方就是白色珠子可以当红色或蓝色的用。
一个坑点:千万不要用一下代码:
string a;
char b[500]={"1","2",···"500"};
for (int i=0;i<500;i++){
a[i]=b[i];
}
程序会爆的!
还有小心开头是w的情况
不说了,上代码:
(代码写的不是很好,用了很多特判,如果有哪位大神能改进,请在评论区指出)
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
using namespace std;
string word;char temp[500];
int n,sum=0,max1=-1;//debug
void fuc (int left,int right){
while (1){
//坑点*1
if (left==0){
if (temp[left]=='w'){
for (int i=0;i<n;i++){
if (temp[i]=='r'||temp[i]=='b'){
temp[0]=temp[i];
break;
}
}
}
}
left++;
sum++;
if (temp[left]=='w') temp[left]=temp[left-1];
if (temp[left-1]!=temp[left]){
left--;
break;
}
}
while (1){
//坑点*2
if (right==n-1){
if (temp[right]=='w'){
for (int i=n-1;i>=0;i--){
if (temp[i]=='r'||temp[i]=='b'){
temp[n-1]=temp[i];
break;
}
}
}
}
if (right==left){
break;
}
right--;
sum++;
if (temp[right]=='w') temp[right]=temp[right+1];
if (temp[right]!=temp[right+1]){
right++;
break;
}
}
}
int main(){
cin>>n;
cin>>word;
for (int i=0;i<n-1;i++){
for (int j=i;j>=0;j--){
temp[i-j]=word[j];
}
for (int j=i+1;j<=n-1;j++){
temp[n-1-(j-i-1)]=word[j];
}
fuc (0,n-1);
if (sum>max1){
max1=sum;
}
sum=0;
}
cout<<max1<<endl;
return 0;
}