字母数字缩写是由字符和数字混合形成的。该数字的值是被省略的字符数。可能有任意数量的被省略的子字符串。没有两个子字符串是相邻的。让我们看一下获取这个概念的算法。
算法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() - '0'); //count the integer value of that digit str.pop_back(); //remove last character } } char to_char = (char)(count + '0'); //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);}
输出hellohell1hel1ohel2he1lohe1l1he2ohe3h1lloh1ll1h1l1oh1l2h2loh2l1h3oh41ello1ell11el1o1el21e1lo1e1l11e2o1e32llo2ll12l1o2l23lo3l14o5
以上就是在c程序中,字符串的字母数字缩写是什么?的详细内容。
