65,197
社区成员




#include<iostream>
#include<string>
using namespace std;
//
int main()
{
string s;
string::iterator iter;
string::reverse_iterator riter;
int num=0;
char rch,lch;
cout<<"Input the string:"<<endl;
cin>>s;
iter=--s.end();
riter=++s.rbegin();
while(*iter=='w')
{
++iter;
if(iter==s.end()) iter=s.begin();
}
rch=*iter;
iter=--s.end();
while(*riter=='w')
{
++riter;
if(riter==s.rend()) riter=s.rbegin();
}
lch=*riter;
iter=--s.end();
riter=++s.rbegin();
while(*iter==rch||*iter=='w')
{
++num;
++iter;
if(iter==s.end())iter=s.begin();
}
while(*riter==lch || *riter=='w')
{
++num;
++riter;
if(riter==s.rend()) riter=s.rbegin();
}
cout<<endl<<num<<endl;
return 0;
}
其中有一组测试数据
77
rwrwrwrwrwrwrwrwrwrwrwrwbwrwbwrwrwrwrwrwrwrwrwrwrwrwrwrwrw
rwrwrwrwrwrwrwrwrwr
答案是72(正确)
不是74(错误)
77-5=72
#include <stdio.h>
#include <string.h>
int getMax(int a, int b)
{
return a > b ? a : b;
}
int main()
{
int n,max;
int toLeft[1000][2];
int toRight[1000][2];
char str[500];
char tmp[1000];
scanf("%d%*c",&n);
gets(str);
strcpy(tmp,str);
strcat(tmp,str);
int i;
toRight[2*n][0] = 0;
toRight[2*n][1] = 0;
toLeft[0][0] = 0;
toLeft[0][1] = 0;
for(i = 1; i < 2*n; ++i)
{
if(tmp[i-1] == 'b')
{
toLeft[i][0] = toLeft[i-1][0] + 1;
toLeft[i][1] = 0;
}
else if(tmp[i-1] == 'r')
{
toLeft[i][0] = 0;
toLeft[i][1] = toLeft[i-1][1] + 1;
}
else if(tmp[i-1] == 'w')
{
toLeft[i][1] = toLeft[i-1][1] + 1;
toLeft[i][0] = toLeft[i-1][0] + 1;
}
}
for(i = n*2 - 1; i >= 0; --i)
{
if(tmp[i] == 'b')
{
toRight[i][0] = toRight[i+1][0] + 1;
toRight[i][1] = 0;
}
else if(tmp[i] == 'r')
{
toRight[i][0] = 0;
toRight[i][1] = toRight[i+1][1] + 1;
}
else if(tmp[i] == 'w')
{
toRight[i][1] = toRight[i+1][1] + 1;
toRight[i][0] = toRight[i+1][0] + 1;
}
}
max = 0;
int l,r;
for(i = 0; i < n*2; ++i)
{
l = getMax(toLeft[i][0],toLeft[i][1]);
r = getMax(toRight[i][0],toRight[i][1]);
max = getMax(max, l+r);
}
if(max > n) max = n;
printf("%d\n",max);
return 0;
}
#include<iostream>
#include<string>
using namespace std;
bool IsRB(string str) //看str中是否同时含有b和r
{
int r=0,b=0;
for(string::size_type i=0;i!=str.size();++i)
{
if(str[i]=='r')
r=1;
if(str[i]=='b')
b=1;
}
if(b==1&&r==1)
return true;
else
return false;
}
int main()
{
int beads(string::size_type i,string str);
int n;
int max=0;
string str;
cin>>n;
cin>>str;
if(IsRB(str))
{
string str1=str;
str1.append(str); //将该字符串扩大一倍 达到循环的效果
for(string::size_type i=0;i!=str1.size();++i) //找该点的前向
{
int pcnt=0,ncnt=0;
for(string::size_type prev=i;prev!=-1;prev--)
{
char sprev=str1[i];
if(str1[prev]==sprev||str1[prev]=='w')
pcnt++;
else
break;
}
for(string::size_type next=i+1;next!=str1.size();++next)//找该点的后向
{
char snext=str1[i+1];
if(str1[next]==snext||str1[next]=='w')
ncnt++;
else
break;
}
if(max<(pcnt+ncnt))
max=pcnt+ncnt;
}
}
else
max=n;
cout<<max<<endl;
return 0;
}
}