برنامه بررسی افزونگی در جریان رشته ها — راهنمای کاربردی

۱۴ بازدید
آخرین به‌روزرسانی: ۷ تیر ۱۴۰۲
زمان مطالعه: ۳ دقیقه
برنامه بررسی افزونگی در جریان رشته ها -- راهنمای کاربردی

در این مطلب، چگونگی نوشتن برنامه بررسی افزونگی در جریان رشته ها مورد بررسی قرار گرفته و پیاده‌سازی آن در زبان‌های گوناگون شامل ++C، «جاوا» (Java)، «پایتون» (Python) و #C انجام شده است. آرایه arr[]‎ از رشته‌ها داده شده است. این آرایه، حاوی اسامی کارکنان یک سازمان است. فرض می‌شود که اسامی یکی پس از دیگری در سیستم وارد شده‌اند. هدف، بررسی این است که آیا نام کنونی، برای اولین بار وارد شده است یا خیر. برای درک بهتر این مطلب، مثال‌های زیر قابل توجه هستند.

Input: arr[] = {“geeks”, “for”, “geeks”}
Output:
No
No
Yes

Input: arr[] = {“abc”, “aaa”, “cba”}
Output:
No
No
No

در این راستا، ابتدا باید یک مجموعه نامرتب (unordered_set) برای ذخیره‌سازی اسامی کارکنان ساخت و کار را با پیمایش در آرایه آغاز کرد. اگر نام کنونی در حال حاضر در آرایه وجود داشته باشد، Yes و در غیر این صورت، No چاپ می‌شود و نام در مجموعه درج می‌شود.

در ادامه، روش پیاده‌سازی رویکرد بالا، بیان شده است.

برنامه بررسی افزونگی در جریان رشته ها در ++C

// C++ implementation of the approach 
#include <bits/stdc++.h> 
using namespace std; 
  
// Function to insert the names 
// and check whether they appear 
// for the first time 
void insertNames(string arr[], int n) 
{ 
  
    // To store the names 
    // of the employees 
    unordered_set<string> set; 
    for (int i = 0; i < n; i++) { 
  
        // If current name is appearing 
        // for the first time 
        if (set.find(arr[i]) == set.end()) { 
            cout << "No\n"; 
            set.insert(arr[i]); 
        } 
        else { 
            cout << "Yes\n"; 
        } 
    } 
} 
  
// Driver code 
int main() 
{ 
    string arr[] = { "geeks", "for", "geeks" }; 
    int n = sizeof(arr) / sizeof(string); 
  
    insertNames(arr, n); 
  
    return 0; 
} 

برنامه بررسی افزونگی در جریان رشته ها در جاوا

// Java implementation of the approach 
import java.util.*; 
  
class GFG  
{ 
  
// Function to insert the names 
// and check whether they appear 
// for the first time 
static void insertNames(String arr[], int n) 
{ 
  
    // To store the names 
    // of the employees 
    HashSet<String> set = new HashSet<String>(); 
    for (int i = 0; i < n; i++)  
    { 
  
        // If current name is appearing 
        // for the first time 
        if (!set.contains(arr[i])) 
        { 
            System.out.print("No\n"); 
            set.add(arr[i]); 
        } 
        else 
        { 
            System.out.print("Yes\n"); 
        } 
    } 
} 
  
// Driver code 
public static void main(String[] args) 
{ 
    String arr[] = { "geeks", "for", "geeks" }; 
    int n = arr.length; 
  
    insertNames(arr, n); 
} 
} 
  
// This code contributed by PrinciRaj1992

برنامه بررسی افزونگی در جریان رشته ها در پایتون ۳

# Python3 implementation of the approach 
  
# Function to insert the names  
# and check whether they appear  
# for the first time  
def insertNames(arr, n) :  
  
    # To store the names  
    # of the employees  
    string = set(); 
      
    for i in range(n) : 
  
        # If current name is appearing  
        # for the first time  
        if arr[i] not in string : 
            print("No");  
            string.add(arr[i]);  
      
        else :  
            print("Yes");  
      
# Driver code  
if __name__ == "__main__" :  
  
    arr = [ "geeks", "for", "geeks" ];  
    n = len(arr);  
  
    insertNames(arr, n);  
  
# This code is contributed by AnkitRai01 

برنامه بررسی افزونگی در جریان رشته ها در #C

// C# implementation of the approach 
using System; 
using System.Collections.Generic; 
  
class GFG  
{ 
  
// Function to insert the names 
// and check whether they appear 
// for the first time 
static void insertNames(String []arr, int n) 
{ 
  
    // To store the names 
    // of the employees 
    HashSet<String> set = new HashSet<String>(); 
    for (int i = 0; i < n; i++)  
    { 
  
        // If current name is appearing 
        // for the first time 
        if (!set.Contains(arr[i])) 
        { 
            Console.Write("No\n"); 
            set.Add(arr[i]); 
        } 
        else
        { 
            Console.Write("Yes\n"); 
        } 
    } 
} 
  
// Driver code 
public static void Main(String[] args) 
{ 
    String []arr = { "geeks", "for", "geeks" }; 
    int n = arr.Length; 
  
    insertNames(arr, n); 
} 
} 
  
// This code is contributed by Rajput-Ji

خروجی قطعه کدهای بالا، به صورت زیر است.

No
No
Yes

اگر نوشته بالا برای شما مفید بوده است، آموزش‌های زیر نیز به شما پیشنهاد می‌شوند:

^^

بر اساس رای ۰ نفر
آیا این مطلب برای شما مفید بود؟
شما قبلا رای داده‌اید!
اگر بازخوردی درباره این مطلب دارید یا پرسشی دارید که بدون پاسخ مانده است، آن را از طریق بخش نظرات مطرح کنید.
منابع:
GeeksforGeeks

نظر شما چیست؟

نشانی ایمیل شما منتشر نخواهد شد. بخش‌های موردنیاز علامت‌گذاری شده‌اند *