- A -> 0
- B -> 1
- C -> 10
- D -> 11
- E -> 100
- F -> 101
- G -> 110
- H -> 111
一行由0和1组成的字符串
一行一个数字表示答案,即解码方法数量
11
2
有D和BB两种解法
100
3
有E,BAA和CA三种解法
输入字符串长度范围为1~100输出解码方法数不超过2147483647
const str = readline() const towArr = ["10","11"] const threeArr = ["100","101","110","111"] const dp = new Array(str.length); dp[0] = 1 dp[1] = 1 for(let i = 2; i<=str.length;i++){ dp[i] = dp[i-1] if(towArr.indexOf(str.substring(i-2,i))!== -1) dp[i] = dp[i]+dp[i-2] if(i>2 && threeArr.indexOf(str.substring(i-3,i))!==-1) dp[i] = dp[i] + dp[i-3] } console.log(dp[str.length])