zoukankan      html  css  js  c++  java
  • USACO 5.4 Character Recognition

    Character Recognition

    This problem requires you to write a program that performs character recognition.

    Each ideal character image has 20 lines of 20 digits. Each digit is a `0' or a `1'. See Figure 1a (way below) for the layout of character images in the file.

    The file font.in contains representations of 27 ideal character images in this order:

    _abcdefghijklmnopqrstuvwxyz

    where _ represents the space character. Each ideal character is 20 lines long.

    The input file contains one or more potentially corrupted character images. A character image might be corrupted in these ways:

    • at most one line might be duplicated (and the duplicate immediately follows)
    • at most one line might be missing
    • some 0's might be changed to 1's
    • some 1's might be changed to 0's.

    No character image will have both a duplicated line and a missing line. No more than 30% of the 0's and 1's will be changed in any character image in the evaluation datasets.

    In the case of a duplicated line, one or both of the resulting lines may have corruptions, and the corruptions may be different.

    Write a program to recognize the sequence of one or more characters in the image provided in the input file using the font provided in file font.in.

    Recognize a character image by choosing the font character images that require the smallest number of overall changed 1's and 0's to be corrupted to the given font image, given the most favourable assumptions about duplicated or omitted lines. Count corruptions in only the least corrupted line in the case of a duplicated line. You must determine the sequence of characters that most closely matches the input sequence (the one that requires the least number of corruptions). There is a unique best solution for each evaluation dataset.

    A correct solution will use precisely all of the data supplied in the input file.

    PROGRAM NAME: charrec

    INPUT FORMAT (both input files)

    Both input files begin with an integer N (19 <= N < 1200) that specifies the number of lines that follow:

    N
    (digit1)(digit2)(digit3) ... (digit20)
    (digit1)(digit2)(digit3) ... (digit20)
    ...

    Each line of data is 20 digits wide. There are no spaces separating the zeros and ones.

    The file font.in describes the font. It will always contain 541 lines. It may differ for each evaluation dataset.

    SAMPLE INPUT (file charrec.in)

    Incomplete sample showing the
    beginning of font.in
    (space and a).

    Sample charrec.in, showing
    an a corrupted

    font.in

    charrec.in

    540
    00000000000000000000
    00000000000000000000
    00000000000000000000
    00000000000000000000
    00000000000000000000
    00000000000000000000
    00000000000000000000
    00000000000000000000
    00000000000000000000
    00000000000000000000
    00000000000000000000
    00000000000000000000
    00000000000000000000
    00000000000000000000
    00000000000000000000
    00000000000000000000
    00000000000000000000
    00000000000000000000
    00000000000000000000
    00000000000000000000
    00000000000000000000
    00000000000000000000
    00000000000000000000
    00000011100000000000
    00000111111011000000
    00001111111001100000
    00001110001100100000
    00001100001100010000
    00001100000100010000
    00000100000100010000
    00000010000000110000
    00000001000001110000
    00001111111111110000
    00001111111111110000
    00001111111111000000
    00001000000000000000
    00000000000000000000
    00000000000000000000
    00000000000000000000
    00000000000000000000
    19
    00000000000000000000
    00000000000000000000
    00000000000000000000
    00000011100000000000
    00100111011011000000
    00001111111001100000
    00001110001100100000
    00001100001100010000
    00001100000100010000
    00000100000100010000
    00000010000000110000
    00001111011111110000
    00001111111111110000
    00001111111111000000
    00001000010000000000
    00000000000000000000
    00000000000001000000
    00000000000000000000
    00000000000000000000
    Figure 1a Figure 1b

    OUTPUT FORMAT

    Your program must produce an output file that contains a single string of the characters recognized. Its format is a single line of ASCII text. The output should not contain any separator characters. If your program does not recognize a particular character, it must output a ? in the appropriate position.

    SAMPLE OUTPUT (file charrec.out)

    a
    

    Note that the 'sample output' formerly displayed a blank followed by an 'a', but that seems wrong now. 

    ————————————————————————————————————————题解

    任何不会做的题一定不能虚的坦然自若地说完题解

    这道题写模拟一定会超时,然后我们发现有最优子结构

    也就是dp[m]表示匹配到了第m行最小的不相符字符数,我们再开一个数组c[j][k]表示从第j行下匹配了k行的最小代价,同时记录这个字符是什么

    20行直接匹配,19行枚举pass掉该字符标准表示的哪一行,21行枚举pass掉匹配的这21行的哪一行然后匹配

    然后记搜转移,因为并不是所有状态都要用到,最后一个点是0.9s才过

    【把font.in点开后粘到编辑器然后看缩略图会发现极为良心的……当然近视眼只需要摘下眼镜了……】

      1 /*
      2 ID: ivorysi
      3 LANG: C++
      4 PROG: charrec
      5 */
      6 #include <iostream>
      7 #include <cstdio>
      8 #include <cstring>
      9 #include <algorithm>
     10 #include <queue>
     11 #include <set>
     12 #include <vector>
     13 #include <string.h>
     14 #include <cmath>
     15 #include <stack>
     16 #include <map>
     17 #define siji(i,x,y) for(int i=(x);i<=(y);++i)
     18 #define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
     19 #define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
     20 #define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
     21 #define inf 0x3f3f3f3f
     22 //#define ivorysi
     23 #define mo 97797977
     24 #define hash 974711
     25 #define base 47
     26 #define pss pair<string,string>
     27 #define MAXN 5000
     28 #define fi first
     29 #define se second
     30 #define pii pair<int,int>
     31 #define esp 1e-8
     32 typedef long long ll;
     33 using namespace std;
     34 char *os=" abcdefghijklmnopqrstuvwxyz?";
     35 char ch[545][25],word[1205][25];
     36 int n1,n2;
     37 int c[1205][25],b[1205],pr[1205][25];
     38 void init() {
     39     FILE *fin=fopen("font.in","r");
     40     fscanf(fin,"%d",&n1);
     41     siji(i,1,n1) {
     42         fscanf(fin,"%s",ch[i]+1);
     43     }
     44     scanf("%d",&n2);
     45     siji(i,1,n2) {
     46         scanf("%s",word[i]+1);
     47     }
     48     siji(i,0,n2) {
     49         b[i]=-1;
     50         siji(j,19,21) {
     51             c[i][j]=-1;
     52         }
     53     }
     54     b[0]=0;
     55 }
     56 int check(int d,int l) {
     57     if(c[d][l]!=-1) return c[d][l];
     58     c[d][l]=inf;
     59     int t=0,t1;
     60     int id1,id2;
     61     int ti=max(20,l);
     62     siji(i,0,26) {
     63         t=0;
     64         if((i+1)*20>n1) break;
     65         siji(j,1,l) {
     66             siji(z,1,20) {
     67                 if(ch[i*20+j][z]!=word[d+j][z]) 
     68                     ++t;
     69             }
     70         }
     71         if(l==21) id1=-1,id2=0;
     72         if(l==19) id1=0,id2=-1;
     73         siji(k,1,20) {
     74             if(l==20) break;
     75             t1=0;
     76             siji(j,1,k-1) {
     77                 siji(z,1,20) {
     78                     if(ch[i*20+j][z]!=word[d+j][z]) ++t1;
     79                 }
     80             }
     81             siji(j,k+1,ti) {
     82                 siji(z,1,20) {
     83                     if(ch[i*20+j+id1][z]!=word[d+j+id2][z]) 
     84                         ++t1;
     85                 }
     86             }
     87             t=min(t,t1);
     88         }
     89         if(c[d][l]>t) {
     90             c[d][l]=t;
     91             pr[d][l]=i;
     92         }
     93     }
     94     if(c[d][l]>120) {pr[d][l]=27;}//假如损坏率超过30%
     95     return c[d][l];
     96 }
     97 
     98 int dfs(int m) {
     99     if(b[m]!=-1) return b[m];
    100     b[m]=inf;
    101     siji(i,19,21) {
    102         if(m-i<0) break;
    103         if(dfs(m-i)>=inf) continue;
    104         b[m]=min(b[m],dfs(m-i)+check(m-i,i));
    105     }
    106     return b[m];
    107 }
    108 void pra(int m) {
    109     if(m==0) return;
    110     char cw;
    111     siji(i,19,21) {
    112         if(b[m]==b[m-i]+c[m-i][i]) {
    113             pra(m-i);
    114             cw=pr[m-i][i];
    115             break;
    116         }
    117     }
    118     printf("%c",os[cw]);
    119 }
    120 void solve() {
    121     init();
    122     dfs(n2);
    123     pra(n2);
    124     puts("");
    125 }
    126 int main(int argc, char const *argv[])
    127 {
    128 #ifdef ivorysi
    129     freopen("charrec.in","r",stdin);
    130     freopen("charrec.out","w",stdout);
    131 #else
    132     freopen("f1.in","r",stdin);
    133 #endif
    134     solve();
    135     return 0;
    136 }
  • 相关阅读:
    使用phantomjs进行刷商务通对话
    利用python打造自己的ftp暴力破解工具
    notepad++开发中常用的插件
    织梦重装漏洞其实并不是那么好利用
    织梦开启调试模式
    网站安全开发人员不可缺少的火狐插件
    dos批量替换当前目录后缀名
    wpf 帧动画
    C 语言 mmap
    C 语言 ioctl
  • 原文地址:https://www.cnblogs.com/ivorysi/p/6420698.html
Copyright © 2011-2022 走看看