import java.text.DecimalFormat; import java.util.*; public class Main { public static double max(double[] array) { double result = Double.MIN_VALUE; for(int i = 0; i < array.length; i++) { if(array[i] > result) { result = array[i]; } } return result; } public static int argmax(double[] array) { int result = 0; double maxValue = Double.MIN_VALUE; for(int i = 0; i < array.length; i++) { if(array[i] > maxValue) { maxValue = array[i]; result = i; } } return result; } public static void main(String[] args) throws IOException { Scanner in = new Scanner(System.in); while(in.hasNext()) { double[] pi = new double[4]; double[][] A = new double[4][4]; double[][] B = new double[4][5]; double prob = 0.0; for(int i = 0; i < 4; i++) pi[i] = in.nextDouble(); for(int i = 0; i < 4; i++) { for (int j = 0; j < 4; j++) A[i][j] = in.nextDouble(); } for(int i = 0; i < 4; i++) { for (int j = 0; j < 5; j++) B[i][j] = in.nextDouble(); } String a = in.next(); String line = in.nextLine(); line = a + line; String[] o = line.split(" "); int[] observe = new int[o.length]; for(int i = 0; i < o.length; i++) { if(o[i].equals("S")) observe[i] = 0; else if(o[i].equals("A")) observe[i] = 1; else if(o[i].equals("B")) observe[i] = 2; else if(o[i].equals("C")) observe[i] = 3; else observe[i] = 4; } double[][] x = new double[observe.length][4]; int T = observe.length; for(int i = 0; i < 4; i++) { x[0][i] = pi[i] * B[i][observe[0]]; } for(int t = 1; t < T; t++) { for(int i = 0; i < 4; i++) { double tmp = 0.0; for(int j = 0; j < 4; j++) { tmp += x[t - 1][j] * A[j][i]; } x[t][i] = tmp * B[i][observe[t]]; } } prob = x[T - 1][0] + x[T - 1][1] + x[T - 1][2] + x[T - 1][3]; DecimalFormat df = new DecimalFormat("#.0000"); System.out.println(df.format(Math.log10(prob))); } } } 一开始没看清题,写成了Viterbi,浪费了半个多小时,吐血
点赞 4

相关推荐

牛舌:如果我不想去,不管对方给了多少,我一般都会说你们给得太低了。这样他们就会给下一个offer的人更高的薪资了。
点赞 评论 收藏
分享
牛客网
牛客企业服务