注册 登录
编程论坛 C++教室

Usaco中beads这个有点问题

lyj23 发布于 2012-01-18 15:28, 448 次点击
程序代码:
/*
ID: liuyuan3
PROG: beads
LANG: C++
*/
#include<iostream>
#include<string>
#include<fstream>
#include<cstdlib>
using namespace std;
string beads;
bool q=1;
bool on=0;
bool if_(char x,char c){

 bool s;


 s=((x==c)||(x=='w'));


 return s;
}
int main(){

 ofstream fou("beads.out");

 ifstream fin("beads.in");

 if(fou.fail()){
  cout<<"open error"<<endl;
  exit(1);

 }

 if(fin.fail()){cout<<"open error"<<endl;
  exit(1);

 }

 //打开文件
int N;

 fin>>N;


 fin>>beads;

 //文件输入
int i,j,max=0;

 for(i=1;i<=N;i++){

 int count=0;

 if(beads[i]=='w'){
  j=i+1;while(beads[j]=='w')j++;
  char c=beads[j];
  //beads[i]=='w'
  on=true;
  count=j-i+1;
  for(;j<N;j++){
   count++;                        //debug
   if((!if_(beads[j],c))||(j==N-1)){
   if(max<count)max=count;
   break;
  
   }

  
   }
   j=i-1;while(beads[j]=='w')j--;
   c=beads[j];                       //c
   count+=i-j+1;
  

 
  for(;j>=0;j--)
     {
   count++;
   if(!if_(beads[j],c)){
   if(max<count)max=count;
   break;
  
   }
  
  
   }
   if(j==-1)
    if(max<count)max=count;
   q=!q;
   //------------------------


 }else{

 //else--------------------------------------
on=false;

 char d=beads[i];

 for(j=i;j<N;j++){
   count++;
   if((!if_(beads[j],d))||(j==N-1)){
   if(max<count)max=count;
   break;
  
   }
  
  
   }
   //count=count-1;
   d=beads[i-1];
  for(j=i-1;j>=0;j--)
     {
  
   if(!if_(beads[j],d)){
   if(max<count)max=count;
   break;
  
   }
   count++;          //放在这里
  
   }
   if(j==-1)
    if(max<count)max=count;

 }

 }

 fou<<max<<endl;

 return 0;
}
这个代码只适合在不会因环形而有问题的情况下。如果是环形的就错误了。。

怎么把它弄成环形!
 代码较长,帮忙仔细看看!主要要改的地方就是几个for循环,循环变量是j的地方

[ 本帖最后由 lyj23 于 2012-1-18 15:29 编辑 ]
0 回复
1