- 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
source = input()
encode = {"0", "1", "10", "11", "100", "101", "110", "111"}
def solve(source):
if len(source) <= 1:
return len(source)
dp = [0] * (len(source) + 1)
dp[0], dp[1] = 1, 1
dp[2] = 1 + (1 if source[:2] in encode else 0)
for i in range(3, len(dp)):
for j in range(3):
k = i - j - 1
if source[k:i] in encode:
dp[i] += dp[k]
return dp[-1] % 2**32
print(solve(source)) import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner=new Scanner(System.in);
String text = scanner.nextLine();
int n=text.length();
String[] words=new String[]{"0","1","10","11","100","101","110","111"};
long[] dp=new long[n+1];
dp[0]=1;
for (int i = 1; i <= n; i++) {
for (int j = 0; j < words.length; j++) {
if(text.substring(0,i).endsWith(words[j])){
dp[i]+=dp[i-words[j].length()];
}
}
}
System.out.println(dp[n]);
}
} 我就说为啥只过了41/50,原来超出int范围了 import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.io.IOException;
public class Main {
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
char[] str = br.readLine().toCharArray();
int n = str.length;
int[] dp = new int[n + 1];
dp[n] = 1; // 最后一个字符肯定只能是一种翻译
// 从后往前遍历字符
for(int i = n - 1; i >= 0; i--){
dp[i] = dp[i + 1]; // 单字符码的情况
if(str[i] == '1'){ // 对于"1",还有双字符码和三字符码的情况
if(i + 2 <= n) dp[i] += dp[i + 2];
if(i + 3 <= n) dp[i] += dp[i + 3];
}
}
System.out.println(dp[0]);
}
} import java.util.*;
public class Main{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
String str = sc.nextLine();
int length = str.length();
int[] dp = new int[length+2];
for(int i=0;i<length+2;i++) dp[i]=0;
for(int i=0;i<length;i++){
if (i==0){
dp[i] = 1;
if("1".equals(str.substring(i,i+1))){
dp[i+1] += 1;
dp[i+2] += 1;
}
}else{
dp[i] += dp[i-1];
if("1".equals(str.substring(i,i+1))){
dp[i+1] += dp[i-1];
dp[i+2] += dp[i-1];
}
}
}
System.out.println(dp[length-1]);
}
} #include<bits/stdc++.h>
using namespace std;
int main(){
string s;
cin>>s;
int n = s.size();
if(n==0){
cout<<0;
return 0;
}
int dp[n+1];
memset(dp,0,sizeof(dp));
dp[0] = 1;
for(int i=1;i<=n;i++){
if(i>=1) dp[i]+=dp[i-1];
if(i>=2&&s[i-2]=='1') dp[i] += dp[i-2];
if(i>=3&&s[i-3]=='1') dp[i] += dp[i-3];
}
cout<<dp[n];
return 0;
} function getSolveCount(str) {
const dp = Array(str.length + 1);
dp[0] = 1;
dp[1] = 1;
const MAX_VALUE = 2147483647 + 1;
for (var i = 2; i < dp.length; i++) {
dp[i] = dp[i - 1];
dp[i] = dp[i] % MAX_VALUE;
if (str[i - 2] !== "0") {
dp[i] += dp[i - 2];
dp[i] = dp[i] % MAX_VALUE;
}
if (i >= 3 && str[i - 3] !== "0") {
dp[i] += dp[i - 3];
dp[i] = dp[i] % MAX_VALUE;
}
}
return dp[str.length];
}
const str = readline();
const res = getSolveCount(str);
print(res); 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])
def count_decodings(s): # 摩尔斯电码和字符映射关系 morse_to_char = { '0': 'A', '1': 'B', '10': 'C', '11': 'D', '100': 'E', '101': 'F', '110': 'G', '111': 'H' } n = len(s) dp = [0] * (n + 1) dp[0] = 1 # 空字符串的解码方法数为 1 for i in range(1, n + 1): # 检查每个可能的字符长度(1到3位) for length in range(1, 4): if i - length >= 0: sub_str = s[i - length:i] if sub_str in morse_to_char: dp[i] += dp[i - length] return dp[n] # 示例 binary_string = "011100" print(count_decodings(binary_string)) # 输出解码方法的数量
package _12月在家重刷.题目;
import java.util.*;
/**
* @author qxm
* @create 2023-03-06 17:44
**/
public class T30摩尔斯电码解码 {
//1:dp
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String str = sc.next();
long[] dp = new long[str.length()];//dp[i]:i到str.len-1的子字符串有dp[i]种解密方式
dp[str.length() - 1] = 1;
for (int i = str.length() - 2; i >= 0; i--) {
dp[i] = dp[i + 1];
if (str.charAt(i) == '1') {
if (i + 1 < str.length()) {
if (i + 2 == str.length()) {
dp[i]++;
} else {
dp[i] += dp[i + 2];
}
}
if (i + 2 < str.length()) {
if (i + 3 == str.length()) {
dp[i]++;
} else {
dp[i] += dp[i + 3];
}
}
}
}
System.out.println(dp[0]);
}
//2:回溯超时G(但是如果要求所有的解密结果,必须要回溯)
static Map<String, String> map = new HashMap<>();
//static List<String> path = new ArrayList<>();
static int ans = 0;
public static void main1(String[] args) {
map.put("0", "A");
map.put("1", "B");
map.put("10", "C");
map.put("11", "D");
map.put("100", "E");
map.put("101", "F");
map.put("110", "G");
map.put("111", "H");
Scanner sc = new Scanner(System.in);
String str = sc.next();
backtracking(0, str);
System.out.println(ans);
}
public static void backtracking(int index, String arr) {
if (index > arr.length()) {
return;
}
if (index == arr.length()) {
ans++;
//System.out.println(path);
return;
}
for (int i = 0; i <= 2 && index + i <= arr.length() - 1; i++) {
String sub = arr.substring(index, index + i + 1);
if (map.containsKey(sub)) {
//path.add(sub);
backtracking(index + i + 1, arr);
//path.remove(path.size() - 1);
}
}
}
}
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
String code=sc.nextLine();
System.out.println(deCode(code));
}
private static long deCode(String code) {
char[] codes=code.toCharArray();
int n=codes.length;
long[] dp=new long[n+1];
//注意初始化为1种
dp[0]=1;
dp[1]=1;dp[2]=codes[0]=='0'?1:2;
for (int i = 3; i < n+1; i++) {
dp[i]=dp[i-1];
if(codes[i-2]=='1'){
dp[i] += dp[i-2];
}
if(codes[i-3]=='1'){
dp[i] += dp[i-3];
}
}
return dp[n];
}
} import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String line = in.nextLine();
in.close();
line = new StringBuilder(line).reverse().toString(); // 将字符串进行翻转这样就比较直观
long[] dp = new long[line.length() + 1]; // dp[i]代表长度为 i 产生的编码数 - long数组防止溢出
dp[0] = 0; // 长度为0的字符串产生编码种类为 0
dp[1] = 1; // 长度为1的字符串产生编码种类为 1
for (int i = 2; i <= line.length(); i++) { // 从长度为2开始迭代
char c = line.charAt(i - 1);
dp[i] = dp[i - 1];
if (c == '1') {
if(i - 2 >= 0) dp[i] += dp[i - 2];
if(i - 3 >= 0) dp[i] += dp[i - 3];
}
}
System.out.println(dp[line.length()]);
}
}
import java.util.*;
public class Main{
public static void main(String[] args){
Scanner scanner=new Scanner(System.in);
char[] list = scanner.next().toCharArray();
System.out.println(Main.count(list)+"");
}
static int count(char[] list){
int[] dp=new int[list.length];
dp[0]=1;
if(list.length==1) return dp[0];
dp[1]= list[0]=='1'? 2:1;
if(list.length==2) return dp[1];
dp[2]=dp[1];
if(list[0]=='1') dp[2]+=1;
if(list[1]=='1') dp[2]+=dp[0];
for(int i=3;i<list.length;++i){
dp[i]=dp[i-1];
if(list[i-1]=='1') dp[i]+=dp[i-2];
if(list[i-2]=='1') dp[i]+=dp[i-3];
}
return dp[list.length-1];
}
} import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;
public class Main {
static Map<String, Character> m = new HashMap<>();
public static void main(String[] args) throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String s = br.readLine();
m.put("0", 'A');
m.put("1", 'B');
m.put("10", 'C');
m.put("11", 'D');
m.put("100", 'E');
m.put("101", 'F');
m.put("110", 'G');
m.put("111", 'H');
int[] dp = new int[s.length() + 1];
dp[0] = 1;
dp[1] = 1;
for (int i = 1; i < s.length(); i++) {
int k = i - 1, l = i - 2;
dp[i + 1] = dp[i];
String substring = s.substring(k, i + 1);
Character character = m.get(substring);
if (character != null)
dp[i + 1] += dp[i - 1];
if (l > -1) {
String str = s.substring(l, i + 1);
Character c = m.get(str);
if (c != null)
dp[i + 1] += i - 2 > 0 ? dp[i - 2] : 1;
}
}
System.out.println(dp[s.length()]);
}
} arr = list(int(n) for n in input()) n = len(arr) res = [0 for x in range(0,n+1)] res[-1] = 1 for i in range(n-1,-1,-1): #从后往前 res[i] = res[i+1] if arr[i] == 1: #单字节码 if i + 2 <= n: res[i] += res[i + 2] if i + 3 <= n: res[i] += res[i + 3] #这个地方如果不取模过不了样例,41/50.感觉应该是解法已经超过2^32了,超出int类型,判断出问题了 print(res[0] % 2**32 )