📌  相关文章
📜  根据另一个数组定义的顺序打印一个数组的元素。设置2

📅  最后修改于: 2021-04-29 05:23:25             🧑  作者: Mango

给定两个数组a1 []和a2 [],以使元素之间的相对顺序与a2中的顺序相同的方式打印a1的元素。也就是说,在数组a2 []中位于前面的元素会首先从数组a1 []中打印这些元素。对于a2中不存在的元素,最后按排序顺序打印它们。

还假定a2 []中的元素数小于或等于a1 []中的元素数,并且a2 []具有所有不同的元素。

例子:

简单方法:我们创建一个临时数组和一个访问数组,其中该临时数组用于将a1 []的内容复制到该数组,而访问数组用于标记该临时数组中复制到a1 []的那些元素。然后对临时数组进行排序,并对a1 []中a2 []的每个元素进行二进制搜索。您可以在此处找到解决方案。

高效的方法:我们可以在O(mlog(n))时间使用C++中的map根据a2 []定义的顺序打印a1 []的元素。我们遍历a1 []并将每个数字的频率存储在映射中。然后,我们遍历a2 []并检查地图中是否存在该数字。如果存在该数字,则将其打印多次并从地图上删除该数字。当数字按排序顺序存储在地图中时,顺序打印地图中存在的其余数字。

下面是上述方法的实现:

C++
// A C++ program to print an array according
// to the order defined by another array
#include 
using namespace std;
  
// Function to print an array according
// to the order defined by another array
void print_in_order(int a1[], int a2[], int n, int m)
{
    // Declaring map and iterator
    map mp;
    map::iterator itr;
  
    // Store the frequncy of each
    // number of a1[] int the map
    for (int i = 0; i < n; i++)
        mp[a1[i]]++;
  
    // Traverse through a2[]
    for (int i = 0; i < m; i++) {
        // Check whether number
        // is present in map or not
  
        if (mp.find(a2[i]) != mp.end()) {
            itr = mp.find(a2[i]);
  
            // Print that number that
            // many times of its frequncy
            for (int j = 0; j < itr->second; j++)
                cout << itr->first << " ";
            mp.erase(a2[i]);
        }
    }
  
    // Print those numbers that are not
    // present in a2[]
    for (itr = mp.begin(); itr != mp.end(); itr++) {
        for (int j = 0; j < itr->second; j++)
            cout << itr->first << " ";
    }
  
    cout << endl;
}
  
// Driver code
int main()
{
    int a1[] = { 2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8 };
    int a2[] = { 2, 1, 8, 3 };
    int n = sizeof(a1) / sizeof(a1[0]);
    int m = sizeof(a2) / sizeof(a2[0]);
  
    print_in_order(a1, a2, n, m);
  
    return 0;
}


Python3
# A Python3 program to print an array according 
# to the order defined by another array 
  
# Function to print an array according 
# to the order defined by another array 
def print_in_order(a1, a2, n, m) : 
  
    # Declaring map and iterator 
    mp = dict.fromkeys(a1,0); 
  
    # Store the frequncy of each 
    # number of a1[] int the map 
    for i in range(n) :
        mp[a1[i]] += 1; 
  
    # Traverse through a2[] 
    for i in range(m) :
        # Check whether number 
        # is present in map or not 
  
        if a2[i] in mp.keys() :
  
            # Print that number that 
            # many times of its frequncy 
            for j in range(mp[a2[i]]) : 
                print(a2[i],end=" "); 
                  
            del(mp[a2[i]]); 
  
    # Print those numbers that are not 
    # present in a2[] 
    for key,value in mp.items() : 
        for j in range(value) :
            print(key,end=" "); 
  
    print(); 
  
  
# Driver code 
if __name__ == "__main__" : 
  
    a1 = [ 2, 1, 2, 5, 7, 1, 9, 3, 6, 8, 8 ]; 
    a2 = [ 2, 1, 8, 3 ]; 
    n =len(a1); 
    m = len(a2); 
  
    print_in_order(a1, a2, n, m); 
      
    # This code is contributed by AnkitRai01


输出:
2 2 1 1 8 8 3 5 6 7 9