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

۹۱
۱۴۰۲/۰۴/۷
۳ دقیقه
PDF
برنامه بررسی افزونگی در جریان رشته ها – راهنمای کاربردیبرنامه بررسی افزونگی در جریان رشته ها – راهنمای کاربردی
آموزش متنی جامع
امکان دانلود نسخه PDF

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

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

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

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

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

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

1// C++ implementation of the approach 
2#include <bits/stdc++.h> 
3using namespace std; 
4  
5// Function to insert the names 
6// and check whether they appear 
7// for the first time 
8void insertNames(string arr[], int n) 
9{ 
10  
11    // To store the names 
12    // of the employees 
13    unordered_set<string> set; 
14    for (int i = 0; i < n; i++) { 
15  
16        // If current name is appearing 
17        // for the first time 
18        if (set.find(arr[i]) == set.end()) { 
19            cout << "No\n"; 
20            set.insert(arr[i]); 
21        } 
22        else { 
23            cout << "Yes\n"; 
24        } 
25    } 
26} 
27  
28// Driver code 
29int main() 
30{ 
31    string arr[] = { "geeks", "for", "geeks" }; 
32    int n = sizeof(arr) / sizeof(string); 
33  
34    insertNames(arr, n); 
35  
36    return 0; 
37} 
مشاهده کامل کدها

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

1// Java implementation of the approach 
2import java.util.*; 
3  
4class GFG  
5{ 
6  
7// Function to insert the names 
8// and check whether they appear 
9// for the first time 
10static void insertNames(String arr[], int n) 
11{ 
12  
13    // To store the names 
14    // of the employees 
15    HashSet<String> set = new HashSet<String>(); 
16    for (int i = 0; i < n; i++)  
17    { 
18  
19        // If current name is appearing 
20        // for the first time 
21        if (!set.contains(arr[i])) 
22        { 
23            System.out.print("No\n"); 
24            set.add(arr[i]); 
25        } 
26        else 
27        { 
28            System.out.print("Yes\n"); 
29        } 
30    } 
31} 
32  
33// Driver code 
34public static void main(String[] args) 
35{ 
36    String arr[] = { "geeks", "for", "geeks" }; 
37    int n = arr.length; 
38  
39    insertNames(arr, n); 
40} 
41} 
42  
43// This code contributed by PrinciRaj1992
مشاهده کامل کدها

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

1# Python3 implementation of the approach 
2  
3# Function to insert the names  
4# and check whether they appear  
5# for the first time  
6def insertNames(arr, n) :  
7  
8    # To store the names  
9    # of the employees  
10    string = set(); 
11      
12    for i in range(n) : 
13  
14        # If current name is appearing  
15        # for the first time  
16        if arr[i] not in string : 
17            print("No");  
18            string.add(arr[i]);  
19      
20        else :  
21            print("Yes");  
22      
23# Driver code  
24if __name__ == "__main__" :  
25  
26    arr = [ "geeks", "for", "geeks" ];  
27    n = len(arr);  
28  
29    insertNames(arr, n);  
30  
31# This code is contributed by AnkitRai01 
مشاهده کامل کدها

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

1// C# implementation of the approach 
2using System; 
3using System.Collections.Generic; 
4  
5class GFG  
6{ 
7  
8// Function to insert the names 
9// and check whether they appear 
10// for the first time 
11static void insertNames(String []arr, int n) 
12{ 
13  
14    // To store the names 
15    // of the employees 
16    HashSet<String> set = new HashSet<String>(); 
17    for (int i = 0; i < n; i++)  
18    { 
19  
20        // If current name is appearing 
21        // for the first time 
22        if (!set.Contains(arr[i])) 
23        { 
24            Console.Write("No\n"); 
25            set.Add(arr[i]); 
26        } 
27        else
28        { 
29            Console.Write("Yes\n"); 
30        } 
31    } 
32} 
33  
34// Driver code 
35public static void Main(String[] args) 
36{ 
37    String []arr = { "geeks", "for", "geeks" }; 
38    int n = arr.Length; 
39  
40    insertNames(arr, n); 
41} 
42} 
43  
44// This code is contributed by Rajput-Ji
مشاهده کامل کدها

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

No
No
Yes

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

^^

بر اساس رای ۱ نفر
آیا این مطلب برای شما مفید بود؟
اگر پرسشی درباره این مطلب دارید، آن را با ما مطرح کنید.
منابع:
GeeksforGeeks
PDF
مطالب مرتبط
نظر شما چیست؟

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