📌  相关文章
📜  根据表示字符串所需的火柴数量对字符串排序

📅  最后修改于: 2021-05-17 06:21:51             🧑  作者: Mango

给定N个字符串的数组arr [] ,任务是根据表示它们所需的木棍数对这些字符串进行排序。
例子:

方法:我们的想法是通过计算每个字符串为每个字符所需要的枝数的帮助下所需棒的数量。然后,将木棍和字符串的数量存储在成对的数组中。最后,根据所需的棍棒数量对它们进行分类。
下图显示了代表每个字符所需的摇杆数:

下面是上述方法的实现:

C++
// C++ implementation to sort the
// strings with the number of
// sticks required to represent them
 
#include 
 
using namespace std;
 
// Stick[] stores the count
// of sticks required to
// represent the alphabets
int sticks[] = { 6, 7, 4, 6, 5, 4, 6,
                 5, 2, 4, 4, 3, 6, 6,
                 6, 5, 7, 6, 5, 3, 5,
                 4, 6, 4, 3, 4 };
 
// Number[] stores the count
// of sticks required to
// represent the numerals
int number[] = { 6, 2, 5, 5, 4, 5, 6,
                 3, 7, 6 };
 
// Function that return the count of
// sticks required to represent
// the given string str
int countSticks(string str)
{
    int cnt = 0;
 
    // Loop to iterate over every
    // character of the string
    for (int i = 0; str[i]; i++) {
 
        char ch = str[i];
 
        // Add the count of sticks
        // required to represent the
        // current character
        if (ch >= 'A' && ch <= 'Z') {
            cnt += sticks[ch - 'A'];
        }
        else {
            cnt += number[ch - '0'];
        }
    }
    return cnt;
}
// Function to sort the array
// according to the number of
// sticks required to represent it
void sortArr(string arr[], int n)
{
    // Vector to store the number
    // of sticks required with
    // respective strings
    vector > vp;
 
    // Inserting number of sticks
    // with respective strings
    for (int i = 0; i < n; i++) {
        vp.push_back(
            make_pair(countSticks(arr[i]),
                      arr[i]));
    }
 
    // Sort the vector,
    sort(vp.begin(), vp.end());
 
    // Print the sorted vector
    for (int i = 0; i < vp.size(); i++)
        cout << vp[i].second << " ";
}
 
// Driver Code
int main()
{
    string arr[] = { "GEEKS", "FOR",
                     "GEEKSFORGEEKS" };
    int n = sizeof(arr) / sizeof(arr[0]);
 
    sortArr(arr, n);
 
    return 0;
}


Java
// Java implementation to sort the
// strings with the number of
// sticks required to represent them
 
import java.io.*;
import java.util.*;
 
class GFG{
    // Stick[] stores the count
    // of sticks required to
    // represent the alphabets
    static int sticks[] = { 6, 7, 4, 6, 5, 4, 6,
                    5, 2, 4, 4, 3, 6, 6,
                    6, 5, 7, 6, 5, 3, 5,
                    4, 6, 4, 3, 4 };
     
    // Number[] stores the count
    // of sticks required to
    // represent the numerals
    static int number[] = { 6, 2, 5, 5, 4, 5, 6,
                    3, 7, 6 };
     
    // Function that return the count of
    // sticks required to represent
    // the given string str
     
    static class Pair implements Comparable{
        int num_sticks;
        String str;
         
        Pair(int n, String s)
        {
            num_sticks = n;
            str=s;
        }
         
        public int compareTo(Pair p)
        {
            return this.num_sticks-p.num_sticks;
        }
    }
    static int countSticks(String str)
    {
        int cnt = 0;
        int n=str.length();
         
        // Loop to iterate over every
        // character of the string
        for (int i = 0; i < n; i++) {
     
            char ch = str.charAt(i);
     
            // Add the count of sticks
            // required to represent the
            // current character
            if (ch >= 'A' && ch <= 'Z') {
                cnt += sticks[ch - 'A'];
            }
            else {
                cnt += number[ch - '0'];
            }
        }
        return cnt;
    }
     
    // Function to sort the array
    // according to the number of
    // sticks required to represent it
    static void sortArr(String arr[], int n) 
    {
        // ArrayList to store the number
        // of sticks required with
        // respective strings
        ArrayList list = new ArrayList<>();
     
        // Inserting number of sticks
        // with respective strings
        for (int i = 0; i < n; i++) {
            list.add(
                new Pair(countSticks(arr[i]),
                        arr[i]));
        }
     
        // Sort the list,
        Collections.sort(list);
     
        // Print the sorted vector
        for (int i = 0; i < list.size(); i++)
            System.out.print(list.get(i).str + " ");
    }
     
    // Driver Code
    public static void main(String []args)
    {
        String arr[] = { "GEEKS", "FOR",
                        "GEEKSFORGEEKS" };
        int n = arr.length;
     
        sortArr(arr, n);
    }
}


Python3
# Python3 implementation to sort the
# strings with the number of
# sticks required to represent them
 
# Stick[] stores the count
# of sticks required to
# represent the alphabets
sticks = [6, 7, 4, 6, 5, 4, 6,
          5, 2, 4, 4, 3, 6, 6,
          6, 5, 7, 6, 5, 3, 5,
          4, 6, 4, 3, 4]
 
# Number[] stores the count
# of sticks required to
# represent the numerals
number = [6, 2, 5, 5, 4,
          5, 6, 3, 7, 6]
 
# Function that return the count of
# sticks required to represent
# the given string str
def countSticks(st):
 
    cnt = 0
 
    # Loop to iterate over every
    # character of the string
    for i in range(len(st)):
 
        ch = st[i]
 
        # Add the count of sticks
        # required to represent the
        # current character
        if (ch >= 'A' and ch <= 'Z'):
            cnt += sticks[ord(ch) -
                          ord('A')]       
        else :
            cnt += number[ord(ch ) -
                          ord('0')]
        
    return cnt
 
# Function to sort the array
# according to the number of
# sticks required to represent it
def sortArr(arr, n):
 
    # Vector to store the number
    # of sticks required with
    # respective strings
    vp = []
 
    # Inserting number of sticks
    # with respective strings
    for i in range(n):
        vp.append([countSticks(arr[i]),
                               arr[i]])
    
    # Sort the vector
    vp.sort()
 
    # Print the sorted vector
    for i in range(len(vp)):
        print (vp[i][1], end = " ")
 
# Driver Code
if __name__ == "__main__":
 
    arr = ["GEEKS", "FOR",
           "GEEKSFORGEEKS"]
    n = len(arr)
    sortArr(arr, n)
 
 # This code is contributed by Chitranayal


输出:
FOR GEEKS GEEKSFORGEEKS




时间复杂度: O(N * log N)