- 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
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.*; 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.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]); } }
第一想法递归,但超时了;
之后看到的是动态,马上就明白(就是一道标准dp问题)
遍历到str[i]时,考虑i,i-1,i-2的情况
importjava.util.HashMap; importjava.util.HashSet; importjava.util.Scanner; publicclassMain{ publicstaticvoidmain(String[] args) { Scanner scanner =newScanner(System.in); String str = scanner.next(); HashSet<String> res =newHashSet<>(); // 0,1不用加 res.add("10"); res.add("11"); res.add("100"); res.add("101"); res.add("110"); res.add("111"); int[] dp =newint[str.length()]; dp[0] =1; for(inti =1; i < str.length(); i++) { dp[i] = dp[i -1]; if(res.contains(str.substring(i -1, i +1))) { dp[i] += i -2>=0? dp[i -2] :1; } if(i -2>=0&& res.contains(str.substring(i -2, i +1))) { dp[i] += i -3>=0? dp[i -3] :1; } } System.out.println(dp[str.length()-1]); } }
import java.io.*; public class Main { public static void main(String[] args) throws Exception { BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); char[] str = br.readLine().toCharArray(); int[] dp = new int[str.length + 1]; dp[0] = 1; dp[1] = 1; for (int i = 1;i < str.length;i++) { dp[i + 1] = dp[i]; if (i - 1 >= 0 && str[i - 1] == '1') { dp[i + 1] += dp[i - 1]; } if (i - 2 >= 0 && str[i - 2] == '1') { dp[i + 1] += dp[i - 2]; } } System.out.printf("%d", dp[str.length]); return; } }