Lintcode 819. Word sort

Source: Internet
Author: User
Tags lintcode

Give a new alphabet, such as {c,b,a,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z}, an array of strings sorted according to the new alphabet.

Precautions
    • The word entered does not exceed the length 100 .
    • The total number of words entered does not exceed 10000 .
    • It can be assumed that the new alphabet entered is one 长度为26的字符串 .
    • Guarantee that the title will only appear 小写字母 .

Sample Example

Given Alphabet = {c,b,a,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z} , String array = {cab,cba,abc} , returns {cba,cab,abc} .

解释:根据新的字典序,排序输出{cba,cab,abc}。

Given Alphabet = {z,b,a,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,c} , String array = {bca,czb,za,zba,ade} , returns {zba,za,bca,ade,czb} .

解释:根据新的字典序,排序输出{zba,za,bca,ade,czb}。

Idea: equivalent to the size of the string redefinition, very quickly can think of a method is to write a comparison function and then throw to Std::sort to sort, but here the comparison function needs to include the comparison dictionary information to proceed, as if only to write a quick row to complete, and the class write comparison function to note the this pointer , you can only use the static function, this needs to be noted. Comparison dictionary I used a simple array here, so small-scale data if the hash structure unordered_map seems a bit overqualified, and finally string_comp this function

1 classSolution {2  Public:3     /**4 * @param alphabet:the new Alphabet5 * @param words:the original string array6 * @return: The string array after sorting7      */8vector<string> Wordsort (string&alphabet, vector<string> &words) {9         //Write Your code hereTen          for(inti =0; i< -; ++i) {//build the dictionary, in the private field Onedict[alphabet[i]-'a'] =i; A         } -vector<string> res =words; -Quicksort_vector_index (Res,0, Words.size ()-1); the         returnRes; -     } -      -     BOOLString_comp (stringS1,stringS2) {//Compare functions, note that only if S1<S2 is returned true +         intSize1 =s1.size (); -         intSize2 =s2.size (); +          for(inti =0; I<min (SIZE1,SIZE2); ++i) { A             if(dict[s1[i]-'a']<dict[s2[i]-'a'])return true; at             Else if(dict[s1[i]-'a']==dict[s2[i]-'a'])Continue; -             Else return false; -         } -         if(SIZE2&GT;SIZE1)return true; -         return false; -     } in      -     voidQuicksort_vector_index (vector<string> &words,intStart,intEnd) {//the modified Quick platoon ... It's kinda interesting. to         if(Start>=end)return; +         stringtemp =Words[start]; -         intL = start;//L,r, around the sentry . the         intR =end; *          while(l<R) { $              while(L<r &&!string_comp (words[r],temp))//find the first number on the right that's smaller than temp and put it in the left dug hole .Panax Notoginsengr--; -             if(L<r)//Note that you must increase the l<r this judgment statement thewords[l++] =Words[r]; +              while(L<r &&!)String_comp (Temp,words[l])) Al++; the             if(l<R) +words[r--] =Words[l]; -         } $WORDS[L] = temp;//Middle Pit, fill in the middle. $Quicksort_vector_index (words,start,l-1); -Quicksort_vector_index (words,l+1, end); -     } the Private: -     intdict[ -]={0};Wuyi};

Lintcode 819. Word sort

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.