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