給定字串在陣列[l, r]範圍內出現的次數


簡介

在本教程中,我們將使用C++實現示例,以查詢輸入字串在一個範圍為[l,r]的陣列中出現的次數。為了解決此任務,字串陣列中僅使用小寫字母。不同的字串儲存在字串陣列中,並遍歷以檢查特定字串是否存在。這是針對給定的L和R範圍。L和R是陣列的起始和結束索引值,用於在輸入字串陣列中搜索該範圍內的字串。查詢位於輸入陣列的L和R之間的字串。

演示1

str_arr[] = {“abc”, “def”, “ghi”} 
str = “abc”
L = 1, R = 2

輸出

0

在上面的演示中,L和R的值分別為1和2。我們檢查字串“abc”是否在str_arr[1, 2]範圍內存在於str_arr中。從索引值為1的起始值迭代str_array[],並搜尋字串“abc”,直到索引值為2。我們發現“abc”在[1, 2]之間不存在,因為它存在於索引值為0的位置。因此,輸出為0。

演示2

str_arr = {“abc”, “ghi”, “cde”} 
str = “cde”
L = 1, R = 3

輸出

1

在上面的演示中,我們檢查字串“cde”是否在str_arr[1, 3]範圍內存在於str_arr中。迭代str_arr以檢查其所有元素的給定範圍。“cde”存在於str_arr[1, 2]中。因此,輸出為1。

示例中使用的C++庫函式

語法

sizeof() : 它是C++中的一個關鍵字,用於確定運算子、資料型別和變數的大小。它在編譯時估算大小。

sizeof(data type);

unordered_map() : 它是C++標準庫中的一個容器類,定義在標頭檔案中。它以無序的方式儲存元素,並以鍵值對的形式儲存。鍵值對中的每個鍵都是唯一的,並具有其關聯的值。

unordered_map<data_type> nameofunordered_map;

unordered_map::find() : 它是unordered_map類的成員函式。它在unordered_map鍵值對中搜索特定鍵。

unordered_map.find(key); 

unordered_map::end() : 它是unordered_map類的成員函式,定義在``標頭檔案中。它返回指向unordered_map最後一個元素之後位置的迭代器。

unordered_map.end(value);

vector() : 它是C++中大小無界的陣列,為其元素提供連續的記憶體位置。它定義在``標頭檔案中。

vector <data_type>vector_name;

upper_bound() : 它指的是儲存元素的下一個位置(或迭代器),該位置大於指定值。

upper_bound(value);

begin() : 它返回儲存在向量陣列中的元素的第一個位置。它是C++庫中的預定義函式。此函式不接受任何引數。

vector_name.begin(); 

make_pair() : 它定義在標準C++模板庫中。它用於使用其第一個和第二個引數建立值對。引數值可以是不同的資料型別。

make_pair(value1, value2);

演算法

  • 初始化一個字串陣列以儲存不同的字串值。

  • 定義一個字串str,在字串陣列中進行檢查。

  • 定義L和R的值。

  • 迭代字串陣列的每個元素以與str進行比較。

  • 取一個計數器變數並將其初始化為0。

  • 如果str與陣列的元素匹配,則增加其值。

  • 返回計數器變數並列印它。

示例1

我們使用C++程式設計概念及其庫函式實現其中一個演示。我們使用一種簡單的方法,定義一個字串陣列“str_arr{}”。對於給定的範圍內的輸入字串str,比較str_arr{}的每個元素。使用計數器變數來計算在str_arr{}中給定範圍內輸入字串的出現次數。

#include <bits/stdc++.h>
using namespace std;
 
// User-defined function to count the number of occurrence of a given string
int totalAppearence(string str_arr[], int a, string s, int L, int R){
      //counter variable
      int cnt=0;
   
      //Iterating for the given values of L and R
    for (int x = L-1; x < R; x++){
          //condition for matches string
          if(str_arr[x]==s)cnt++;
    }
 
    return cnt;
}
 
// Program Controller
int main(){
    string str_arr[] = { "abc", "def", "abc" };
    int a = sizeof(str_arr) / sizeof(string);
    int L = 1;
    int R = 2;
    string s = "abc";
 
    cout << "Number of times input string appear in the array is:" << totalAppearence(str_arr, a, s, L, R);
    return 0;
}

輸出

Number of times input string appear in the array is: 1

示例2

為了實現上述演示之一,我們使用unordered_map來儲存字串陣列的值。unordered_map索引用於在定義的範圍內將輸入字串與陣列元素進行比較。

由於unordered_map具有對映值和鍵值對,因此它有助於快速搜尋。

#include <bits/stdc++.h>
using namespace std;
 
// User-defined function to count the occurrence of input string
int totalAppearence(string str_arr[], int a, string s, int L, int R)
{
    // initialized unordered_map
    unordered_map<string, vector<int> > N;
    for (int x = 0; x < a; x++) 
    {
        string t = str_arr[x];
        auto i = N.find(t);
 
        if (i == N.end()) 
        {
            vector<int> B;
            B.push_back(x + 1);
            N.insert(make_pair(t, B));
        }
        else 
        {
            i->second.push_back(x + 1);
        }
    }
 
    auto i = N.find(s);
 
    // When string is not in array
    if (i == N.end())
        return 0;
 
    //when string is found in array
    vector<int> B = i->second;
    int m = upper_bound(B.begin(), B.end(), R) - B.begin();
    int n = upper_bound(B.begin(), B.end(), L - 1) - B.begin();
 
    return (m - n);
}
 
// program controller
int main()
{
    string str_arr[] = { "abc", "dfe", "cba" };
    int a = sizeof(str_arr) / sizeof(string);
    int L = 1;
    int R = 1;
    string s = "gef";
 
    cout << "Number of times input string appears in the array is : " << totalAppearence(str_arr, a, s, L, R);
 
    return 0;
}

輸出

Number of times input string appears in the array is : 0

結論

我們完成了本教程,以查詢輸入字串在範圍為L和R的字串陣列中出現的次數。演示了該任務,使問題陳述更清晰,便於實現。我們使用不同的C++庫函式實現了兩個示例。在第一個示例中,我們獲取一個字串陣列,並迭代定義範圍內的元素,並檢查它是否與輸入字串匹配。在第二種方法中,使用unordered_map儲存索引。要搜尋輸入字串的出現次數,請搜尋unordered_map索引並列印結果。

更新於:2023年8月18日

64次瀏覽

開啟您的職業生涯

完成課程獲得認證

開始學習
廣告