To prove safety offer- face questions 46- digital translated into strings - Dynamic Programming

/*
topic:
	Given a number of 0 through 25 sequentially translated as a ~ z, there are many kinds of method for calculating the translation.
*/
/*
Ideas:
	Dynamic Programming
*/
#include<iostream>
#include<cstring>
#include<vector>
#include<algorithm>


using namespace std;



int GetTranslationCount(string number){
    int length = number.size();
    if(length == 0 || length == 1){
        return length;
    }
    int n_next = 1; // the next character is the current number
    int next = 2; // current character next character
    if(number.substr(length-2,2) > "25"){
        next = 1;
    }
    int curr = next;
    for(int i = length - 3; i >= 0; i--){
        if(number.substr(i,2) > "25"){
            curr = next;
        }else{
            curr = next + n_next;
        }
        n_next = next;
        next = curr;
    }
    return curr;
}

int main () {
    string a = "12258";
    cout<<GetTranslationCount(a);
    return 0;
}

   

Guess you like

Origin www.cnblogs.com/buaaZhhx/p/12031474.html
Recommended