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 |
Sample |
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点开后粘到编辑器然后看缩略图会发现极为良心的……当然近视眼只需要摘下眼镜了……】
/*
ID: ivorysi
LANG: C++
PROG: charrec
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <queue>
#include <set>
#include <vector>
#include <string.h>
#include <cmath>
#include <stack>
#include <map>
#define siji(i,x,y) for(int i=(x);i<=(y);++i)
#define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
#define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
#define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
#define inf 0x3f3f3f3f
//#define ivorysi
#define mo 97797977
#define hash 974711
#define base 47
#define pss pair<string,string>
#define MAXN 5000
#define fi first
#define se second
#define pii pair<int,int>
#define esp 1e-8
typedef long long ll;
using namespace std;
char *os=" abcdefghijklmnopqrstuvwxyz?";
char ch[][],word[][];
int n1,n2;
int c[][],b[],pr[][];
void init() {
FILE *fin=fopen("font.in","r");
fscanf(fin,"%d",&n1);
siji(i,,n1) {
fscanf(fin,"%s",ch[i]+);
}
scanf("%d",&n2);
siji(i,,n2) {
scanf("%s",word[i]+);
}
siji(i,,n2) {
b[i]=-;
siji(j,,) {
c[i][j]=-;
}
}
b[]=;
}
int check(int d,int l) {
if(c[d][l]!=-) return c[d][l];
c[d][l]=inf;
int t=,t1;
int id1,id2;
int ti=max(,l);
siji(i,,) {
t=;
if((i+)*>n1) break;
siji(j,,l) {
siji(z,,) {
if(ch[i*+j][z]!=word[d+j][z])
++t;
}
}
if(l==) id1=-,id2=;
if(l==) id1=,id2=-;
siji(k,,) {
if(l==) break;
t1=;
siji(j,,k-) {
siji(z,,) {
if(ch[i*+j][z]!=word[d+j][z]) ++t1;
}
}
siji(j,k+,ti) {
siji(z,,) {
if(ch[i*+j+id1][z]!=word[d+j+id2][z])
++t1;
}
}
t=min(t,t1);
}
if(c[d][l]>t) {
c[d][l]=t;
pr[d][l]=i;
}
}
if(c[d][l]>) {pr[d][l]=;}//假如损坏率超过30%
return c[d][l];
} int dfs(int m) {
if(b[m]!=-) return b[m];
b[m]=inf;
siji(i,,) {
if(m-i<) break;
if(dfs(m-i)>=inf) continue;
b[m]=min(b[m],dfs(m-i)+check(m-i,i));
}
return b[m];
}
void pra(int m) {
if(m==) return;
char cw;
siji(i,,) {
if(b[m]==b[m-i]+c[m-i][i]) {
pra(m-i);
cw=pr[m-i][i];
break;
}
}
printf("%c",os[cw]);
}
void solve() {
init();
dfs(n2);
pra(n2);
puts("");
}
int main(int argc, char const *argv[])
{
#ifdef ivorysi
freopen("charrec.in","r",stdin);
freopen("charrec.out","w",stdout);
#else
freopen("f1.in","r",stdin);
#endif
solve();
return ;
}
USACO 5.4 Character Recognition的更多相关文章
- USACO 5.4 Character Recognition(DP)
非常恶心的一题,卡了三个月,没什么动力做了,代码直接抄的别人的... 这题主要思路就是预处理出几个数组,再预处理出几个数组,最后DP,输出一下路径... 写起来挺非常麻烦,代码不贴了,丢人... 把U ...
- OCR (Optical Character Recognition,光学字符识别)
OCR (Optical Character Recognition,光学字符识别)是指电子设备(例如扫描仪或数码相机)检查纸上打印的字符,通过检测暗.亮的模式确定其形状,然后用字符识别方法将形状翻译 ...
- csharp:Optical Character Recognition
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- 图片OCR(Optical Character Recognition)
目录 Photo OCR问题描述 滑动窗口(Sliding Windows) 获得大量数据和人工数据(Getting Logs of Data and Artificial Data) 瓶颈分析:需要 ...
- OCR(Optical Character Recognition)算法总结
https://zhuanlan.zhihu.com/p/84815144 最全OCR资料汇总,awesome-OCR
- 第 38 章 OCR - Optical Character Recognition
38.1. Tesseract 查找Tesseract安装包 $ apt-cache search Tesseract ocrodjvu - tool to perform OCR on DjVu d ...
- USACO 5.4 章节
Canada Tour 题目大意 双向连通图,点从左向右排列, 你需要先从最左的点到最右的点,(过程中只能从左向右走) 然后再从最右的点返回最左的点,(过程中只能从右向左走) 过程中除了最左的点,其它 ...
- USACO 完结的一些感想
其实日期没有那么近啦……只是我偶尔还点进去造成的,导致我没有每一章刷完的纪念日了 但是全刷完是今天啦 讲真,题很锻炼思维能力,USACO保持着一贯猎奇的题目描述,以及尽量不用高级算法就完成的题解……例 ...
- Online handwriting recognition using multi convolution neural networks
w可以考虑从计算机的“机械性.重复性”特征去设计“低效的”算法. https://www.codeproject.com/articles/523074/webcontrols/ Online han ...
随机推荐
- webpack+vue+iview使用vue-cli脚手架搭建
1.安装nodejs环境 下载node.js.安装成功后再控制台输入 node -v 出现版本号则,安装成功. 如果没有出现 版本号 而是出现node 不是内部命令的话 需要配置一下环境变量,如果已经 ...
- java的灵魂——反射
前言:当我们在类加载的时候,除了把.class文件加载到内存,会把各个类存到各个类模板(class类型)对象上,相同的类只有一个类模板对象.连接:把类的二进制数据合并到JRE中 反射运行时探究和使用编 ...
- UIScrollView增加回弹效果
项目中经常要增加上下滑动回弹的效果: self.scrollView.alwaysBounceVertical = YES;
- Vue入坑教程(一)——搭建vue-cli脚手架
1. Vue简介 详细内容可以参考官网Vue.js 1)兼容性 Vue 不支持 IE8 及以下版本,因为 Vue 使用了 IE8 无法模拟的 ECMAScript 5 特性.但它支持所有兼容 ECMA ...
- 新建 Vue项目 使用iView报错解决
错误如下: error in ./src/index.less Module build failed: // https://github.com/ant-design/ant-motion/iss ...
- MySQL学习(一)——Java连接MySql数据库
MySQL学习(一)——Java连接MySql数据库 API详解: 获得语句执行 String sql = "Insert into category(cid, cname) values( ...
- Android端抓取日志
一.背景: ADT-Bundlee for Windows 是由GoogleAndroid官方提供的集成式IDE,已经包含了Eclipse,你无需再去下载Eclipse,并且里面已集成了插件,它解决大 ...
- Diabetic Retinopathy Winner's Interview: 1st place, Ben Graham
Diabetic Retinopathy Winner's Interview: 1st place, Ben Graham Ben Graham finished at the top of the ...
- 你知道吗?10个精妙的 Java 编码最佳实践
这是一个比Josh Bloch的Effective Java规则更精妙的10条Java编码实践的列表.和Josh Bloch的列表容易学习并且关注日常情况相比,这个列表将包含涉及API/SPI设计中不 ...
- 数据库(八)之T-SQL编程
什么是Transact-SQL? 结构化查询语言(SQL)是有美国国家标准协会(ANSI)和国际标准化组织(ISO)定义的标准,而Transact-SQL是Microsoft公司对此标准的一个实现. ...