Only one test case is passed ,i think my logic is correct ,please tell me.
Why all test case are not passed
Your code is not visible.What is your logic?
Can you send your code again?
#include <bits/stdc++.h>
using namespace std;
bool compare(string s1,string s2){
int i=0;
if(s1[i]==s2[i]){
return s1.length()>s2.length();
}
return s1<s2;
}
int main()
{
int n;
cin>>n;
cin.get();
string s[1000];
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;
}
My doubt is not resolved yet,please tell me whats wrong in my code,oonly one test case is passed
Your compare function was wrong.You have to compare character by character till first different character is found and if you couldn’t find a different character till min length of the two string then do length comparsion.
I have changed it in the code,Please see it.
Please explain me how compare function work , how we decide it should return greater than aur less than ,if we compare two strings.
Just read it on Google. I won’t be able to type all that and they have examples as well.
Compare function is same as normal one, you have to write the logic for true and false condition.
See my code for more details