滿足給定條件的字串計數


簡介

在 C++ 中,字串也是一種基本資料型別,它由字母數字字元組成。字串中的字母區分大小寫,但字串可以包含大寫和小寫字母。

在本文中,我們得到一個包含小寫字串的輸入陣列,需要計算陣列中滿足以下條件的字串對的數量:

  • 兩個字串都應該具有相同的首尾母音。

  • 兩個字串都具有相同數量的母音對。

陣列是一種模擬儲存相似元素的資料結構。C++ 陣列必須滿足以下屬性:

  • 陣列中的所有元素都必須屬於相同的資料型別。

  • 陣列具有固定長度。

以下是一個關於所提問題的示例:

示例

示例 1 - arr : { “bitch” , “glitch” , “bitter”, “ditch”}

輸出 - 1

解釋 - 滿足條件的字串對是“bitch”和“glitch”,它們都包含母音{i}。這兩個字串的首尾母音都是'i',是等價的。“ditch”也滿足條件,但它已經被包含在一個有效的對中了。

解決這個問題的方法是字元檢查,然後在C++中維護一個元組來儲存母音的計數。

語法

make_tuple ( val1, val2, val3..)

C++中的`make_tuple()`函式用於構造指定型別的元組物件。它用於將值賦給對映中的元組。值按宣告順序賦值。

引數

val1, val2 , .. - 要賦給對映中元組的值。

在評估此方法的過程中使用的另一種方法是:

push_back( val1)

內建的`push_back()`方法用於將元素插入C++資料物件,例如向量或對映。插入總是從末尾進行。每次插入後,物件的大小都會增加一。該方法在恆定時間複雜度內執行。

引數

val - 要插入到C++物件中的值。

演算法

  • 將一個示例字串陣列 arr 作為輸入。

  • 維護一個對映 map 來儲存滿足所需條件的單詞元組。

  • 使用 for 迴圈 i 對 arr 進行迭代。

  • - 在每次迭代中,儲存一個字元向量 vec 來跟蹤從陣列 arr 中獲取的單詞的母音。

  • 使用變數 word 引用陣列中的特定單詞。

  • 對提取的陣列 word 執行另一個迴圈迭代 j。每次都從 word 中提取特定字元,並檢查它是母音還是子音。

  • 如果提取的字元是母音,則將其推入 vec 中。

  • 如果 vec 非空,即存在母音,則從當前單詞 i 中提取首尾母音。

  • 使用提取的母音和第 i 個單詞中的母音總數在對映中構造一個元組,然後將其對映到第 i 個索引。然後使用 `push_back()` 方法將建立的元組新增到對映的末尾。

  • 對對映進行迭代。

  • 計算可以使用對映中元組對形成的可能的對的數量。

  • 然後將對的數量作為輸出返回,由變數 count 反映。

示例

下面的 C++ 程式碼片段說明了計算滿足以下條件的字串的過程:

//include the required libraries
#include <bits/stdc++.h>
using namespace std;

//count number of pairs satisfying the cond
int satisfycondn(string sarr[], int n){
   //declaring map to store tuples
    map<tuple<char, char, int>, vector<int> > map;
 
   // For every string of the array
    for (int i = 0; i < n; i++) {
 
      //storing vowels of the extracted word
      vector<char> vec;
      //extracting the word 
      string word = sarr[i];
      for (int j = 0; j < word.size(); j++) {
         char ch = sarr[i][j];
         //checking if the character is vowel
         if (ch=='a' || ch=='e' || ch=='i' || ch=='o' || ch=='u')
         vec.push_back(ch);
      }
      int numvowels =  vec.size();
      // If current string contains vowels
      if (numvowels > 0) {
         
      int frstvowel = vec[0];
      int scndvowel =  vec[numvowels - 1];
            
      map[make_tuple(frstvowel,  scndvowel, numvowels)]
        .push_back(i);
      }
   }
   //maintaining a count to store the pair of strings satisfying the cond 
   int count = 0;
   //iterating over the map 
   for (auto m : map) {
 
      vector<int> v = m.second;
      //getting the valid pair of size 
      int v_size = v.size();
      //incrementing the counter
       count += v_size / 2;
   }
   return count;
}
 
int main() {
   //declaring a sample array
   string arr[] = { "point", "coin","groin","foul","area", "mourn" };  
   int n =   sizeof(arr) / sizeof(string);
   cout << "Count of strings satisfying the condition : "<<satisfycondn(arr,n);
 
   return 0;
}

輸出

Count of strings satisfying the condition − 2

解釋 - 陣列中有兩對字串滿足條件 - {point, coin} 和 {foul, mourn"}。

第一對字串各有 2 個母音,分別是 o 和 i。

第二對字串各有 2 個母音,分別是 o 和 u。

結論

C++ 中的對映是相當通用的資料結構,用於以更友好的方式儲存資料及其關聯屬性。它簡化了根據指定條件訪問資料。

更新於:2023年7月31日

99 次檢視

開啟您的職業生涯

完成課程後獲得認證

開始學習
廣告