I have written the following code. It is unable to pass all the test cases. Is there any mistake in the code. Please let me know…
include
include
include
using namespace std;
bool compare(string a, string b){
if(a.size() <= b.size() && b.substr(0, a.size())==a){
return false;
}
else if(b.size() <= a.size() && a.substr(0, b.size())==b){
return true;
}
else{
return a<b;
}
}
int main(){
int n;
cin >> n;
cin.get();
string s[100];
for(int i=0; i<n; i++){
getline(cin, s[i]);
}
sort(s, s+n, compare);
for(int i=0; i<n; i++){
cout << s[i] << endl;
}
return 0;
}
