To prove safety offer digital translated into strings

0 -> 'a', 1 -> 'b', ..., 11 -> 'l', ..., 25 -.> 'Z' is calculated how many of a number of different methods of translation.

Analysis: note f [i] from the i-th bit represents the number of different translations.

f [i] = f [i + 1] + g (i, i + 1) * f [i + 2]. g (i, i + 1) if the numbers put together between 10-25, for the 1, and 0 otherwise.

 1 int GetTranslationCount(int number) {
 2     if (number < 0) {
 3         return 0;
 4     }
 5     string numberInstring = to_string(number);
 6     return GetTranslationCount(numberInstring);
 7 }
 8 
 9 int GetTranslationCount(const string& number) {
10     int len = number.length();
11     int *count = new int[length];
12     int count = 0;
13     for (int i = len - 1; i >= 0; i--) {
14         count = 0;
15         if (i < len - 1) {
16             count = counts[i + 1];
17         } else {
18             count = 1;
19         }
20         if (i < len - 1) {
21             int digit1 = numbers[i] - '0';
22             int digit2 = numbers[i + 1] - '0';
23             int converted = digit1 * 10 + digit2;
24             if (converted >= 10 && converted <= 25) {
25                 if (i < len - 2) {
26                     count += counts[i + 2];
27                 } else {
28                     count += 1;
29                 }
30             }
31         }
32         counts[i] = count;
33     }
34     count = counts[0];
35     delete[] counts;
36     return count;
37 }

 

Guess you like

Origin www.cnblogs.com/qinduanyinghua/p/11410080.html