RT,有如下两份代码。
错解( MLE ):
#include<bits/stdc++.h>
using namespace std;
string code(string x){
if(x.find('1')<0) return "A";
else if(x.find('0')<0) return "B";
else return "C"+code(x.substr(0,x.size()/2))+code(x.substr(x.size()/2,x.size()/2));
}
int main(){
string a;
cin>>a;
cout<<code(a);
return 0;
}
正解:
#include<bits/stdc++.h>
using namespace std;
string code(string x){
if(x.find('1')==-1) return "A";
else if(x.find('0')==-1) return "B";
else return "C"+code(x.substr(0,x.size()/2))+code(x.substr(x.size()/2,x.size()/2));
}
int main(){
string a;
cin>>a;
cout<<code(a);
return 0;
}
注意到两份代码中仅有 x.find() 的条件判断从 <0 改为了 ==-1 。经蒟蒻 BDFS 后发现、此现象可能和 find() 函数的特殊返回值有关,故在此求问大佬 find() 函数返回值的详细知识以及错解造成 MLE 的原因。