首頁  >  文章  >  後端開發  >  在C程式中,字串的字母數字縮寫是什麼?

在C程式中,字串的字母數字縮寫是什麼?

王林
王林轉載
2023-09-14 22:25:03702瀏覽

在C程式中,字串的字母數字縮寫是什麼?

在這裡,我們將看到一個與給定字串的字母數字縮寫相關的有趣問題。字串長度小於10。我們將列印出所有的字母數字縮寫。

字母數字縮寫是由字元和數字混合形成的。該數字的值是被省略的字元數。可能有任意數量的被省略的子字串。沒有兩個子字串是相鄰的。讓我們來看看獲取這個概念的演算法。

演算法

printAbbreviation(s, index, max, str) −

begin
   if index is same as max, then
      print str
   end if
   add s[index] at the last of str
   printAbbreviation(s, index + 1, max, str)
   delete last character from str
   count := 1
   if str is not empty, then
      if the last character of str is a digit, then
         add last digit with the count value
         delete last character from str
      end if
   end if
   add count after the str
   printAbbreveation(s, index + 1, max, str)
end

範例

#include <iostream>
using namespace std;
void printAbbreviation(const string& s, int index, int max_index, string str) {
   if (index == max_index) { //if string has ended
      cout << str << endl;
      return;
   }
   str.push_back(s[index]); // push the current character to result
   printAbbreviation(s, index + 1, max_index, str); //print from next index
   str.pop_back(); //remove last character
   int count = 1;
   if (!str.empty()) {
      if (isdigit(str.back())) { //if the last one is digit, then
         count += (int)(str.back() - &#39;0&#39;); //count the integer value of that digit
         str.pop_back(); //remove last character
      }
   }
   char to_char = (char)(count + &#39;0&#39;); //make count to character
   str.push_back(to_char);
   printAbbreviation(s, index + 1, max_index, str); //do for next index
}
void printCombination(string str) {
   if (!str.length()) //if the string is empty
      return;
   string str_res;
   printAbbreviation(str, 0, str.length(), str_res);
}
int main() {
   string str = "HELLO";
   printCombination(str);
}

輸出

#
HELLO
HELL1
HEL1O
HEL2
HE1LO
HE1L1
HE2O
HE3
H1LLO
H1LL1
H1L1O
H1L2
H2LO
H2L1
H3O
H4
1ELLO
1ELL1
1EL1O
1EL2
1E1LO
1E1L1
1E2O
1E3
2LLO
2LL1
2L1O
2L2
3LO
3L1
4O
5

以上是在C程式中,字串的字母數字縮寫是什麼?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:tutorialspoint.com。如有侵權,請聯絡admin@php.cn刪除