给定输入的字符串,计算根据字符串不同组合能生成的不同组合的个数

 1 #include <iostream>
 2 #include <map>
 3 typedef unsigned long long ullong;
 4 using std::string;
 5 
 6 ullong factorial(ullong i) {
 7     if (i > 1) {
 8         return i * factorial(i - 1);
 9     }
10     if (i == 1) {
11         return 1;
12     }
13     if (i == 0) {
14         return 0;
15     }
16 }
17 
18 ullong count(const string &s) {
19     using std::map;
20     map<char, ullong> repeat_nums;
21     for (ullong i = 0; i < s.length(); i++) {
22         repeat_nums[s[i]]++;
23     }
24     ullong result = factorial(s.length());
25     for (auto it = repeat_nums.begin(); it != repeat_nums.end(); it++) {
26         if (it->second > 1) {
27             result /= factorial(it->second);
28         }
29     }
30     return result;
31 }
32 
33 int main() {
34     std::cout << count("ABA") << std::endl;
35     std::cout << count("ABCDEFGHHA") << std::endl;
36     std::cout << count("AABBCC") << std::endl;
37 }

 

posted @ 2019-07-07 18:33  Ren.Yu  阅读(439)  评论(0编辑  收藏  举报