The program was throwing error.I figured out that while loop was not terminating because A.size()-1 resulted in some large no even when A.size() has become 0.
I instead used a variable n to store A.size() and used it in while loop it worked.But I want to know why A.size()-1 prints such large no.
//Input: A:"nnnn"
int i=0;
while(i<A.size()-1){
if(A[i]==A[i+1]){
A.erase(A.begin()+i);
A.erase(A.begin()+i);
if(i!=0)i--;
}
else i++;
cout<<i<<" "<<A.size()-1<<endl;
}
if(A=="")return "empty";
return A;
}
/* //below code works fine
int i=0;
int n=A.size();
while(i<n-1){
if(A[i]==A[i+1]){
A.erase(A.begin()+i);
A.erase(A.begin()+i);
if(i!=0)i--;
}
else i++;
n=A.size();
}
if(A=="")return "empty";
return A;
*/
//output
0 1
0 18446744073709551615
Aucun commentaire:
Enregistrer un commentaire