Nishant is a very naughty boy in Launchpad Batch. One day he was playing with strings, and randomly shuffled them all. Your task is to help Nishant Sort all the strings ( lexicographically ) but if a string is present completely as a prefix in another string, then string with longer length should come first. Eg bat, batman are 2 strings and the string bat is present as a prefix in Batman - then sorted order should have - Batman, bat.
Sample Input
3
bat
apple
batman
Sample Output
apple
batman
bat
The code written by me
using namespace std;
bool compare(string a,string b){
if(a.find(b)){
return a<b;
}
else if(b.find(a)){
return a>b;
}
else return a<b;
}
void sort(string a [],int n,bool compare){
for(int itr=1;itr<n;itr++){
for(int i=0;i<=n-itr-1;i++){
if(a[i]>a[i+1]){
swap(a[i],a[i+1]);
}}
}
for(int i=0;i<n;i++){
cout<<a[i]<<endl;
}
}
int main() {
string a[1000];
int n;
cin>>n;
cin.get();
for(int i=0;i<n;i++){
getline(cin,a[i]);
}
sort(a,n,compare);
return 0;
}
plz tell the mistake as I am not getting correct output