usaco 5.4.2 Character Recognition

来源:互联网 发布:node.js是前端还是后端 编辑:程序博客网 时间:2024/05/29 13:03
/*ID: Daniel.20LANG: JAVATASK: charrec*/import java.util.*;import java.io.*;class pb_charrec{String letters = " abcdefghijklmnopqrstuvwxyz";static int MAXN = 1201;int n;char font[][][] = new char[28][21][21];char input[][] = new char[MAXN][21];int diff[][][] = new int[28][21][MAXN];int cost[][] = new int[MAXN][22];int from[][] = new int[MAXN][22];int best[] = new int[MAXN];int last[] = new int[MAXN];void solver() throws IOException{initial();get_diff();get_cost();dp();output();}void initial() throws IOException{BufferedReader reader = new BufferedReader(new FileReader("font.in"));reader.readLine();for(int i=1;i<28;i++){for(int j=1;j<21;j++){String c = reader.readLine();for(int k=1;k<21;k++)font[i][j][k] = c.charAt(k-1);}}reader.close();reader = new BufferedReader(new FileReader("charrec.in"));n = Integer.valueOf(reader.readLine());for(int i=1;i<=n;i++){String c = reader.readLine();for(int j=1;j<=20;j++)input[i][j] = c.charAt(j-1);}reader.close();//test_print(input,n,20);//test_print(font[2],n,20);}void get_diff(){for(int i=1;i<28;i++){for(int j=1;j<21;j++){for(int k=1;k<=n;k++){for(int t=1;t<21;t++){if(font[i][j][t]!=input[k][t]) diff[i][j][k]++;}}}}}void get_cost(){int total = 0;for(int i=1;i<=n;i++){Arrays.fill(cost[i], Integer.MAX_VALUE);}for(int i=1;i<=n;i++){if(i+18<=n){for(int j=1;j<28;j++){total = 0;for(int k=2;k<=20;k++) total+=diff[j][k][i+k-2];if(total<cost[i][19]){cost[i][19]=total;from[i][19]=j;}for(int k=2;k<=20;k++){total+=diff[j][k-1][i+k-2];total-=diff[j][k][i+k-2];if(total<cost[i][19]){cost[i][19]=total;from[i][19]=j;}}}}if(i+19<=n){for(int j=1;j<28;j++){total = 0;for(int k=1;k<=20;k++) total+=diff[j][k][i+k-1];if(total<cost[i][20]){cost[i][20]=total;from[i][20]=j;}}}if(i+20<=n){for(int j=1;j<28;j++){total = 0;for(int k=1;k<=20;k++) {if(k==1) total+=diff[j][1][i];else total+=diff[j][k][i+k];}if(total<cost[i][21]){cost[i][21]=total;from[i][21]=j;}for(int k=2;k<=20;k++){total+=diff[j][k][i+k-1];total-=diff[j][k][i+k];if(total<cost[i][21]){cost[i][21]=total;from[i][21]=j;}}}}}}void dp(){Arrays.fill(best, Integer.MAX_VALUE);best[0]=0;for(int i=1;i<=n;i++){if(i>=19&&best[i-19]!=Integer.MAX_VALUE){if(best[i-19]+cost[i-18][19]<best[i]){best[i] = best[i-19]+cost[i-18][19];last[i] = 19;}}if(i>=20&&best[i-20]!=Integer.MAX_VALUE){if(best[i-20]+cost[i-19][20]<best[i]){best[i] = best[i-20]+cost[i-19][20];last[i] = 20;}}if(i>=21&&best[i-21]!=Integer.MAX_VALUE){if(best[i-21]+cost[i-20][21]<best[i]){best[i] = best[i-21]+cost[i-20][21];last[i] = 21;}}}}void output() throws IOException{StringBuilder sb = new StringBuilder();int num = 0;int result[] = new int[MAXN];for(int i=n;i>=1;i-=last[i]){result[++num]=from[i-last[i]+1][last[i]];}for(int i=num;i>=1;i--){sb.append(letters.charAt(result[i]-1));}PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("charrec.out")));out.println(sb.toString());out.close();}void test_print(char[][] c, int x, int y){for(int i=1;i<=x;i++){for(int j=1;j<=y;j++)System.out.print(c[i][j]);System.out.println();}}}public class charrec {public static void main(String[] args) throws Exception {pb_charrec p = new pb_charrec();          p.solver();  }}


这死题目再难一点,马勒个臀的,我YY了一下DP但是想不出来

好复杂的各种数组比较

写到第5节的最后已然A不动了,知识面和思维都跟不上。。。悲催

0 0
原创粉丝点击