Home > Back-end >  Why is this the dictionary from big to small, according to the length of the same length according t
Why is this the dictionary from big to small, according to the length of the same length according t

Time:03-12

Leetcode. 524
Why is this the dictionary from big to small, according to the length of the same length according to the dictionary sequence sorting sort is not working?
In local vscode run output is correct "ABC"

The static bool cmp1 (string& A, string& B) {
Return a.s considering () & gt; B.s considering ();
}

The static bool cmp2 (string a, string b) {
Return to a.com pare said (b)==1;
}

String findLongestWord (string s, vector & The dictionary) {
Sort (a dictionary. The begin (), the dictionary. The end (), cmp1);
Int I, j, size=dictionary. The size ();
For (I=j=0; J & lt; The size; I={+ + j)
While (j & lt; The size 1 & amp; & The dictionary [j + 1). The size ()==a dictionary [I] size j++ ());
Sort (a dictionary. The begin () + I, dictionary. The begin () + j + 1, cmp2);
}
}
Type s="abce", dictionary=[" Abe ", "ABC", "asd"] I'm leetcode compiler output for Abe

CodePudding user response:

 # include & lt; Vector> 
# include & lt; string>
# include & lt; Utility>
# include & lt; Algorithm>
# include & lt; iostream>

using namespace std;

The static bool cmp1 (string& A, string& B) {
Bool r;
R=(a.s considering () & gt; B.s considering ());
if (! R) {
If (a.s considering ()==b.s considering ()) {
R=(a.com pare said (b) & lt; 0);
return r;
}
}
return r;
}

String findLongestWord (string s, vector & The dictionary) {
Sort (a dictionary. The begin (), the dictionary. The end (), cmp1);
for (int i=0; iCout}
Return s;
}

Int main () {
String s="abce";
Vector The dictionary;
Dictionary. Push_back (" Abe ");
Dictionary. Push_back (" ABC ");
Dictionary. Push_back (" asd ");
String t=findLongestWord (s, a dictionary);
return 0;
}
//ABC
//Abe
//asd
//
  • Related