AcWing 59. translated into digital strings (the offer to prove safety)

Given a number, we follow the following rules to translate it to a string:

0 translate "a", 1 translated into "b", ......, 11 translated into "l", ......, 25 translated into "z".

There may be more than a digital translation. For example 12258 There are five different translations, they are "bccfi", "bwfi", "bczi", "mcfi" and "mzi".

Please programming function for a number of different methods to calculate a number have translation.

Sample
input: "12258"

Output: 5

water. . .

class Solution {
public:
    int getTranslationCount(string s) {
        int dp[10005];memset(dp,0,sizeof(dp));
        int n = s.size();
        dp[0] = 1;
        for(int i = 1;i <= n;i++) {
            dp[i] = dp[i - 1];
            if(i >= 2) {
                int t = (s[i - 2] - '0') * 10 + (s[i - 1] - '0');
                if(t <= 25 && t >= 10) dp[i] += dp[i - 2];
            }
        }
        return dp[n];
    }
};
Published 844 original articles · won praise 28 · views 40000 +

Guess you like

Origin blog.csdn.net/tomjobs/article/details/104962364