Python Programming Questions-stitching the smallest dictionary order

Source: Internet
Author: User

Source: Niu Ke Net

For a given array of strings, find a concatenation order so that all small strings are stitched into a large string that is the smallest of all possible stitching in the dictionary order.

Given a string array STRs, given its size, return the concatenation of the strings.

Test examples:

["abc", "De"],2
"ABCDE"


Python

Code

#-*-Coding:utf-8-*-class prior:def findsmallest (self, STRs, n): # write code here RESSTR = ""                    For I in Xrange (1,len (STRs)): for J in Xrange (0,i): if (Strs[i]+strs[j]) < (Strs[j]+strs[i]): STRS[I],STRS[J] = Strs[j],strs[i] return '. Join (STRs)


Shell implementations

Code

#/bin/bash#by xianwei#2017-9-4cat <<eof topic for a given array of strings, find a concatenation order so that all small strings are stitched into a large string that is the smallest of all possible stitching in the dictionary order. Given a string array strs, given its size, return the concatenation of the strings. Test sample: ["abc", "De"],2 "ABCDE" eofa= ("Kid" "yqt" "" I "" K ") for ((i=0;i<${#a [*]};i++)] do if [[" j=0;j<i;j++]    a[$i]}${a[$j]} "<" ${a[$j]}${a[$i]} "] then tmp=${a[$i]}echo $tmp a[$i]=${a[$j]} a[$j]= $tmp fi Donedoneecho ${a[*]}



C + + implementation

Code

Class prior {public:    string findsmallest (Vector<string> strs,  int n)  {        // write code here         string A;         for  (Int i = 0; i < strs.size (); i++)          {            for  (int j  = i; j < strs.size ();  j++)              {                 if  ((Strs[i] + strs[j]) > (strs[j] + strs[i])//If k+kid >  Kid +k, Exchange                       swap (strs[i],strs[j]);            }         }        for  (int i  = 0; i < strs.size ();  i++)         {             A = A + strs[i];         }        return a ;    }};


This article is from "operations to Development" blog, please be sure to keep this source http://237085.blog.51cto.com/227085/1962689

Python Programming Questions-stitching the smallest dictionary order

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.