📜  从Array中查找原始序列,该序列包含按顺序合并多次的序列

📅  最后修改于: 2021-05-17 02:11:08             🧑  作者: Mango

给定一个数字N和一个数组arr [] ,该数组arr []N个长度不同的整数的长度序列组成,可以任意次数保持初始序列中元素的相对顺序。任务是找到长度N的初始序列,并保持正确的顺序。

例子:

方法:想法是观察到在给定序列中最先出现的元素是所得恢复序列中的第一个元素。在我们恢复的序列中使用该元素,并且不包括给定序列中的重复元素。对其余元素执行相同的操作,直到到达结尾为止。这个想法可以通过C++中的MapSet来实现。

使用地图

  1. 从左到右遍历给定的序列。
  2. 将考虑序列中首次出现的元素,并使用地图进行标记。
  3. 遍历时标记的元素将被忽略。
  4. 继续执行第2步和第3步,直到到达给定序列的末尾。

下面是上述方法的实现:

C++
// C++ prpgram for the above approach
#include 
using namespace std;
 
// Function that returns the restored
// permutation
vector restore(int arr[], int N)
{
    // Vector to store the result
    vector result;
 
    // Map to mark the elements
    // which are taken in result
    map mp;
    for (int i = 0; i < N; i++) {
 
        // Check if the element is
        // coming first time
        if (mp[arr[i]] == 0) {
 
            // Push in result vector
            result.push_back(arr[i]);
 
            // Mark it in the map
            mp[arr[i]]++;
        }
    }
 
    // Return the answer
    return result;
}
 
// Function to print the result
void print_result(vector result)
{
    for (int i = 0; i < result.size(); i++)
        cout << result[i] << " ";
}
 
// Driver Code
int main()
{
    // Given Array
    int arr[] = { 1, 13, 1, 24, 13, 24, 2, 2 };
 
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    print_result(restore(arr, N));
    return 0;
}


Java
// Java prpgram for the above approach
import java.util.*;
class GFG{
 
// Function that returns the restored
// permutation
static Vector restore(int arr[], int N)
{
    // Vector to store the result
    Vector result = new Vector<>();
 
    // Map to mark the elements
    // which are taken in result
    HashMap mp = new HashMap();
    for (int i = 0; i < N; i++)
    {
 
        // Check if the element is
        // coming first time
        if (mp.containsKey(arr[i]) &&
            mp.get(arr[i]) == 0)
        {
 
            // Push in result vector
            result.add(arr[i]);
 
            // Mark it in the map
            if(mp.containsKey(arr[i]))
            {
                mp.put(arr[i], mp.get(arr[i]) + 1);
            }
            else
            {
                mp.put(arr[i], 1);
            }
        }
        else
            mp.put(arr[i], 0);
    }
 
    // Return the answer
    return result;
}
 
// Function to print the result
static void print_result(Vector result)
{
    for (int i = 0; i < result.size(); i++)
        System.out.print(result.get(i) + " ");
}
 
// Driver Code
public static void main(String[] args)
{
    // Given Array
    int arr[] = { 1, 13, 1, 24, 13, 24, 2, 2 };
 
    int N = arr.length;
 
    // Function Call
    print_result(restore(arr, N));
}
}
 
// This code is contributed by Rajput-Ji


Python3
# Python3 program for the above approach
 
# Function that returns the restored
# permutation
def restore(arr, N):
     
    # List to store the result
    result = []
     
    # Map to mark the elements
    # which are taken in result
    mp = {}
     
    for i in range(N):
         
        # Checking if the element is
        # coming first time
        if not arr[i] in mp:
             
            # Push in result vector
            result.append(arr[i])
             
            # Mark it in the map
            mp[arr[i]] = 1
             
    # Return the answer
    return result
 
# Function to print the result
def print_result(result):
     
    for i in range(len(result)):
        print(result[i], end = " ")
         
# Driver code
def main():
     
    # Given array
    arr = [ 1, 13, 1, 24, 13, 24, 2, 2 ]
    N = len(arr)
     
    # Function call
    print_result(restore(arr, N))
     
main()
     
# This code is contributed by Stuti Pathak


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
 
class GFG{
 
// Function that returns the restored
// permutation
static List restore(int []arr, int N)
{
     
    // List to store the result
    List result = new List();
 
    // Map to mark the elements
    // which are taken in result
    Dictionary mp = new Dictionary();
    for(int i = 0; i < N; i++)
    {
         
        // Check if the element is
        // coming first time
        if (mp.ContainsKey(arr[i]) &&
            mp[arr[i]] == 0)
        {
             
            // Push in result vector
            result.Add(arr[i]);
 
            // Mark it in the map
            if(mp.ContainsKey(arr[i]))
            {
                mp[arr[i]] = mp[arr[i]] + 1;
            }
            else
            {
                mp.Add(arr[i], 1);
            }
        }
        else
            mp.Add(arr[i], 0);
    }
 
    // Return the answer
    return result;
}
 
// Function to print the result
static void print_result(List result)
{
    for(int i = 0; i < result.Count; i++)
        Console.Write(result[i] + " ");
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given Array
    int []arr = { 1, 13, 1, 24, 13, 24, 2, 2 };
 
    int N = arr.Length;
 
    // Function call
    print_result(restore(arr, N));
}
}
 
// This code is contributed by 29AjayKumar


C++
// C++ program for the above approach
#include 
using namespace std;
 
// Function that returns the restored
// permutation
vector restore(int arr[], int N)
{
    // Vector to store the result
    vector result;
    int count1 = 1;
 
    // Set to insert unique elements
    set s;
    for (int i = 0; i < N; i++) {
 
        s.insert(arr[i]);
 
        // Check if the element is
        // coming first time
        if (s.size() == count1) {
 
            // Push in result vector
            result.push_back(arr[i]);
 
            count1++;
        }
    }
 
    return result;
}
 
// Function to print the result
void print_result(vector result)
{
    for (int i = 0; i < result.size(); i++)
        cout << result[i] << " ";
}
 
// Driver Code
int main()
{
    // Given Array
    int arr[] = { 1, 13, 1, 24, 13, 24, 2, 2 };
 
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    print_result(restore(arr, N));
    return 0;
}


Java
// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function that returns the restored
// permutation
static Vector restore(int arr[], int N)
{
     
    // Vector to store the result
    Vector result = new Vector();
     
    int count1 = 1;
 
    // Set to insert unique elements
    HashSet s = new HashSet();
     
    for(int i = 0; i < N; i++)
    {
        s.add(arr[i]);
 
        // Check if the element is
        // coming first time
        if (s.size() == count1)
        {
             
            // Push in result vector
            result.add(arr[i]);
            count1++;
        }
    }
    return result;
}
 
// Function to print the result
static void print_result(Vector result)
{
    for(int i = 0; i < result.size(); i++)
        System.out.print(result.get(i) + " ");
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given Array
    int arr[] = { 1, 13, 1, 24, 13, 24, 2, 2 };
 
    int N = arr.length;
 
    // Function call
    print_result(restore(arr, N));
}
}
 
// This code is contributed by Princi Singh


Python3
# Python3 program for the
# above approach
 
# Function that returns the
# restored permutation
def restore(arr, N):
 
    # Vector to store the
    # result
    result = []
    count1 = 1
 
    # Set to insert unique
    # elements
    s = set([])
     
    for i in range(N):
        s.add(arr[i])
 
        # Check if the element is
        # coming first time
        if (len(s) == count1):
 
            # Push in result vector
            result.append(arr[i])
 
            count1 += 1
 
    return result
 
# Function to print the
# result
def print_result(result):
 
    for i in range(len(result)):
        print(result[i],
              end = " ")
 
# Driver Code
if __name__ == "__main__":
 
    # Given Array
    arr = [1, 13, 1, 24,
           13, 24, 2, 2]
 
    N = len(arr)
 
    # Function Call
    print_result(restore(arr, N))
 
# This code is contributed by Chitranayal


C#
// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
 
// Function that returns the restored
// permutation
static List restore(int []arr, int N)
{
     
    // List to store the result
    List result = new List();
     
    int count1 = 1;
 
    // Set to insert unique elements
    HashSet s = new HashSet();
     
    for(int i = 0; i < N; i++)
    {
        s.Add(arr[i]);
 
        // Check if the element is
        // coming first time
        if (s.Count == count1)
        {
             
            // Push in result vector
            result.Add(arr[i]);
            count1++;
        }
    }
    return result;
}
 
// Function to print the result
static void print_result(List result)
{
    for(int i = 0; i < result.Count; i++)
        Console.Write(result[i] + " ");
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given Array
    int []arr = { 1, 13, 1, 24, 13, 24, 2, 2 };
 
    int N = arr.Length;
 
    // Function call
    print_result(restore(arr, N));
}
}
 
// This code is contributed by PrinciRaj1992


输出:
1 13 24 2


时间复杂度: O(N)
辅助空间: O(N)

使用集

  1. 从左到右遍历给定的序列。
  2. 将获取一个计数器并将其初始化为1。
  3. 将元素一一插入到集合中。如果在某个时候集合和计数器的大小相同,则将元素考虑在内,并将计数器增加1。
  4. 继续执行第3步和第4步,直到到达给定序列的末尾。

下面是上述方法的实现:

C++

// C++ program for the above approach
#include 
using namespace std;
 
// Function that returns the restored
// permutation
vector restore(int arr[], int N)
{
    // Vector to store the result
    vector result;
    int count1 = 1;
 
    // Set to insert unique elements
    set s;
    for (int i = 0; i < N; i++) {
 
        s.insert(arr[i]);
 
        // Check if the element is
        // coming first time
        if (s.size() == count1) {
 
            // Push in result vector
            result.push_back(arr[i]);
 
            count1++;
        }
    }
 
    return result;
}
 
// Function to print the result
void print_result(vector result)
{
    for (int i = 0; i < result.size(); i++)
        cout << result[i] << " ";
}
 
// Driver Code
int main()
{
    // Given Array
    int arr[] = { 1, 13, 1, 24, 13, 24, 2, 2 };
 
    int N = sizeof(arr) / sizeof(arr[0]);
 
    // Function Call
    print_result(restore(arr, N));
    return 0;
}

Java

// Java program for the above approach
import java.util.*;
 
class GFG{
 
// Function that returns the restored
// permutation
static Vector restore(int arr[], int N)
{
     
    // Vector to store the result
    Vector result = new Vector();
     
    int count1 = 1;
 
    // Set to insert unique elements
    HashSet s = new HashSet();
     
    for(int i = 0; i < N; i++)
    {
        s.add(arr[i]);
 
        // Check if the element is
        // coming first time
        if (s.size() == count1)
        {
             
            // Push in result vector
            result.add(arr[i]);
            count1++;
        }
    }
    return result;
}
 
// Function to print the result
static void print_result(Vector result)
{
    for(int i = 0; i < result.size(); i++)
        System.out.print(result.get(i) + " ");
}
 
// Driver Code
public static void main(String[] args)
{
     
    // Given Array
    int arr[] = { 1, 13, 1, 24, 13, 24, 2, 2 };
 
    int N = arr.length;
 
    // Function call
    print_result(restore(arr, N));
}
}
 
// This code is contributed by Princi Singh

Python3

# Python3 program for the
# above approach
 
# Function that returns the
# restored permutation
def restore(arr, N):
 
    # Vector to store the
    # result
    result = []
    count1 = 1
 
    # Set to insert unique
    # elements
    s = set([])
     
    for i in range(N):
        s.add(arr[i])
 
        # Check if the element is
        # coming first time
        if (len(s) == count1):
 
            # Push in result vector
            result.append(arr[i])
 
            count1 += 1
 
    return result
 
# Function to print the
# result
def print_result(result):
 
    for i in range(len(result)):
        print(result[i],
              end = " ")
 
# Driver Code
if __name__ == "__main__":
 
    # Given Array
    arr = [1, 13, 1, 24,
           13, 24, 2, 2]
 
    N = len(arr)
 
    # Function Call
    print_result(restore(arr, N))
 
# This code is contributed by Chitranayal

C#

// C# program for the above approach
using System;
using System.Collections.Generic;
class GFG{
 
// Function that returns the restored
// permutation
static List restore(int []arr, int N)
{
     
    // List to store the result
    List result = new List();
     
    int count1 = 1;
 
    // Set to insert unique elements
    HashSet s = new HashSet();
     
    for(int i = 0; i < N; i++)
    {
        s.Add(arr[i]);
 
        // Check if the element is
        // coming first time
        if (s.Count == count1)
        {
             
            // Push in result vector
            result.Add(arr[i]);
            count1++;
        }
    }
    return result;
}
 
// Function to print the result
static void print_result(List result)
{
    for(int i = 0; i < result.Count; i++)
        Console.Write(result[i] + " ");
}
 
// Driver Code
public static void Main(String[] args)
{
     
    // Given Array
    int []arr = { 1, 13, 1, 24, 13, 24, 2, 2 };
 
    int N = arr.Length;
 
    // Function call
    print_result(restore(arr, N));
}
}
 
// This code is contributed by PrinciRaj1992
输出:
1 13 24 2


时间复杂度: O(N)
辅助空间: O(N)