usaco 5.4 Character Recognition(统计DP)

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 1aFigure 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 output is a line with two characters: a blank followed by an `a'. 

题意:给你一个字符集图形,还有一些有损失的读入,要求输出识别的字符串。。。

分析:完全没搞明白为什么是DP,本来想写暴力匹配的,结果觉得太麻烦了就没写,网上都说是DP,不会啊,最后看别人的代码有点点理解,这里就吐槽下

贴贴代码:

/*
ID: 15114582
PROG: charrec
LANG: C++
*/
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
const int mm=1222;
int d[mm][29][25],f[mm],g[mm],p[mm],s1[25],s2[25];
char w[29][22][25],a[mm][25];
int i,j,k,l,n,m;
int score(int num,int len,int j)
{
    int i,ret=1e9;
    s1[0]=0;
    for(i=1;i<len;++i)s1[i]=s1[i-1]+d[num-len+i][j][i];
    s2[len+1]=0;
    for(i=len;i>1;--i)s2[i]=s2[i+1]+d[num-len+i][j][20-(len-i)];
    for(i=1;i<len;++i)
        ret=min(ret,s1[i]+s2[i+1]);
    return ret;
}
void out(int i)
{
    if(p[i])out(p[i]);
    if(g[i])printf("%c",'a'+g[i]-1);
    else printf(" ");
}
int main()
{
    freopen("font.in","r",stdin);
    scanf("%d",&n);
    for(i=0;i<=26;++i)
        for(j=1;j<21;++j)
            scanf("%s",w[i][j]);
    freopen("charrec.in","r",stdin);
    scanf("%d",&m);
    for(i=1;i<=m;++i)
        scanf("%s",a[i]);
    freopen("charrec.out","w",stdout);
    memset(d,0,sizeof(d));
    memset(g,0,sizeof(g));
    for(i=1;i<=m;++i)
        for(j=0;j<=26;++j)
            for(k=1;k<21;++k)
                for(l=0;l<20;++l)
                    d[i][j][k]+=(w[j][k][l]!=a[i][l]);
    memset(f,100,sizeof(f));
    f[0]=0;
    for(i=19;i<=m;++i)
        for(j=0;j<=26;++j)
            for(l=19;l<22;++l)
                if(i>=l)
                {
                    k=score(i,l,j);
                    if(f[i]>f[i-l]+k)
                    {
                        f[i]=f[i-l]+k;
                        g[i]=j;
                        p[i]=i-l;
                    }
                }
    out(m);
    puts("");
    return 0;
}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值