题目大意是字符串识别
一道细节很繁琐的DP,要用到很多数组
一开始还真看不出是DP,后来参考了别人的代码,然后又按自己的思路重头到尾写了,虽然速度不咋的

Executing...
Test 1: TEST OK [0.008 secs, 6504 KB]
Test 2: TEST OK [0.008 secs, 6504 KB]
Test 3: TEST OK [0.019 secs, 6504 KB]
Test 4: TEST OK [0.035 secs, 6504 KB]
Test 5: TEST OK [0.084 secs, 6504 KB]
Test 6: TEST OK [0.116 secs, 6504 KB]
Test 7: TEST OK [0.167 secs, 6504 KB]
Test 8: TEST OK [0.192 secs, 6504 KB]
Test 9: TEST OK [0.178 secs, 6504 KB]

All tests OK.
YOUR PROGRAM ('charrec') WORKED FIRST TIME! That's fantastic
-- and a rare thing. Please accept these special automated
congratulations.

 #include <stack>
#include <iostream>
#include <stdio.h>
#define R19 0
#define R20 1
#define R21 2
#define INF 1000000000
using namespace std; char ch[][][];
char mat[][];
int dif[][][];
int cost[][]; // cost[i][j]表示从mat的第i行开始匹配j行得到的最小差距
int optimalCh[][]; // optimalCh[i][j]表示从mat的第i行开始匹配j行最优的匹配字符
int d[]={}; // d[i]表示匹配到第i行所得到的最小差距
int step[]; // step[i]表示第i行开始的最优匹配的字符行数
// 得到行数中不同的个数
/*
num为第几个字符
l1为num字符的行数
l2为mat的行数
*/
int getDif(int num,int l1,int l2)
{
int sum=;
for(int i=;i<;i++)
{
if(ch[num][l1][i]!=mat[l2][i])
sum++;
}
return sum;
} // num为字符,l1为mat开始匹配的行数,del为删除了哪一行
int getCost(int num,int l1,int del)
{
int sum=;
for(int i=,p=;p<;i++,p++)
{
if(i==del)
i++;
sum+=dif[num][i][l1+p];
}
return sum;
} int getCost2(int num,int l1,int del)
{
int sum=;
for(int i=,p=;i<;i++,p++)
{
if(p==del)
p++;
sum+=dif[num][i][p+l1];
}
return sum;
} int main()
{
freopen("font.in","r",stdin);
int n; // 行数
cin>>n;
for(int i=;i<n/;i++)
{
for(int j=;j<;j++)
{
scanf("%s",ch[i][j]); }
} freopen("charrec.in","r",stdin);
freopen("charrec.out","w",stdout); cin>>n; for(int i=;i<n;i++)
scanf("%s",mat[i]); // 初始化dif数组
for(int i=;i<;i++)
for(int j=;j<;j++)
{
for(int k=;k<n;k++)
{
dif[i][j][k]=getDif(i,j,k);
}
} // 初始化cost
for(int i=;i<n;i++) // 从第i行开始匹配
{
cost[i][R19]=cost[i][R20]=cost[i][R21]=INF;
for(int j=;j<;j++) // 第j个字符
{
if(i+<n+)
{
for(int k=;k<;k++) // 删掉行数
{
if(cost[i][R19]>getCost(j,i,k))
{
cost[i][R19]=getCost(j,i,k);
optimalCh[i][R19]=j;
}
}
}
if(i+<n+)
{
if(cost[i][R20]>getCost(j,i,-))
{
cost[i][R20]=getCost(j,i,-);
optimalCh[i][R20]=j;
}
}
if(i+<n+)
{
for(int k=;k<;k++) // 多出的行数
{
if(cost[i][R21]>getCost2(j,i,k))
{
cost[i][R21]=getCost2(j,i,k);
optimalCh[i][R21]=j;
}
}
}
}
} fill_n(d,n+,INF);
// 初始值
d[]=cost[][R19];step[]=;
d[]=cost[][R20];step[]=;
d[]=cost[][R21];step[]=;
// DP
for(int i=;i<n;i++) // 匹配的最后一行为i
{
if(d[i-]+cost[i-][R19]<d[i])
{
d[i]=d[i-]+cost[i-][R19];
step[i]=;
}
if(d[i-]+cost[i-][R20]<d[i])
{
d[i]=d[i-]+cost[i-][R20];
step[i]=;
}
if(d[i-]+cost[i-][R21]<d[i])
{
d[i]=d[i-]+cost[i-][R21];
step[i]=;
}
} stack<int> S; for(int p=n-;p!=-;p=p-step[p])
{
S.push(optimalCh[p-step[p]+][step[p]-]);
} while(!S.empty())
{
int tmp=S.top();
S.pop();
printf("%c",tmp==?' ':tmp-+'a');
}
cout<<endl;
return ;
}

USACO5.4-Character Recognition的更多相关文章

  1. USACO 5.4 Character Recognition

    Character Recognition This problem requires you to write a program that performs character recogniti ...

  2. OCR (Optical Character Recognition,光学字符识别)

    OCR (Optical Character Recognition,光学字符识别)是指电子设备(例如扫描仪或数码相机)检查纸上打印的字符,通过检测暗.亮的模式确定其形状,然后用字符识别方法将形状翻译 ...

  3. csharp:Optical Character Recognition

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  4. 图片OCR(Optical Character Recognition)

    目录 Photo OCR问题描述 滑动窗口(Sliding Windows) 获得大量数据和人工数据(Getting Logs of Data and Artificial Data) 瓶颈分析:需要 ...

  5. USACO 5.4 Character Recognition(DP)

    非常恶心的一题,卡了三个月,没什么动力做了,代码直接抄的别人的... 这题主要思路就是预处理出几个数组,再预处理出几个数组,最后DP,输出一下路径... 写起来挺非常麻烦,代码不贴了,丢人... 把U ...

  6. OCR(Optical Character Recognition)算法总结

    https://zhuanlan.zhihu.com/p/84815144 最全OCR资料汇总,awesome-OCR

  7. 第 38 章 OCR - Optical Character Recognition

    38.1. Tesseract 查找Tesseract安装包 $ apt-cache search Tesseract ocrodjvu - tool to perform OCR on DjVu d ...

  8. Online handwriting recognition using multi convolution neural networks

    w可以考虑从计算机的“机械性.重复性”特征去设计“低效的”算法. https://www.codeproject.com/articles/523074/webcontrols/ Online han ...

  9. 车牌识别1:License Plate Detection and Recognition in Unconstrained Scenarios阅读笔记

    一.WHAT 论文下载地址:License Plate Detection and Recognition in Unconstrained Scenarios [pdf] github 的项目地址: ...

  10. Tesseract-OCR字符识别简介

    OCR(Optical Character Recognition):光学字符识别,是指对图片文件中的文字进行分析识别,获取的过程.Tesseract:开源的OCR识别引擎,初期Tesseract引擎 ...

随机推荐

  1. 菜单栏始终浮动在顶部 js

    //菜单栏始终浮动在顶部var navH = $(".trade-tab-bot").offset().top;//获取要定位元素距离浏览器顶部的距离//滚动条事件$(window ...

  2. 手势触摸定位(UIPanGestureRecognizer)

    /** 1.相对于父坐标系而言,表示当前触摸点所在的位置 */ CGPoint locationPoint = [panGestureRecognizer locationInView:panGest ...

  3. (3)选择元素——(15)总结(Summary)

    With the techniques that we have covered in this chapter, we should now be able to locate sets of el ...

  4. RIP协议两个版本号对不连续子网的支持情况实验

    (增加时注明"会员咨询")

  5. 常见算法:C语言求最小公倍数和最大公约数三种算法

    最小公倍数:数论中的一种概念,两个整数公有的倍数成为他们的公倍数,当中一个最小的公倍数是他们的最小公倍数,相同地,若干个整数公有的倍数中最小的正整数称为它们的最小公倍数,维基百科:定义点击打开链接 求 ...

  6. 通过数组初始化链表的两种方法:指向指针的引用node *&tail和指向指针的指针(二维指针)node **tail

    面试高频题:单链表的逆置操作/链表逆序相关文章 点击打开 void init_node(node *tail,char *init_array) 这样声明函数是不正确的,函数的原意是通过数组初始化链表 ...

  7. 挂载(mount)深入理解

    首先引用一句 wiki 上的定义来开篇: Mounting takes place before a computer can use any kind of storage device (such ...

  8. github 预览html

    在网址前加 http://htmlpreview.github.io/?

  9. 【nodejs学习】0.nodejs学习第一天

    1.模块 大一点的程序都需要模块化,nodejs也不例外,代码放到不同的文件中,每一个文件就可以是一个模块,文件路径名就是一个模块名.每个模块中包含三个预先定义的变量: 1.require:用于在当前 ...

  10. [C#]asp.net开发微信公众平台----目录汇总-持续更新

    1.[c#]asp.net微信公众平台开发(1)数据库设计 2.[c#]asp.net微信公众平台开发(2)多层架构框架搭建和入口实现 3.[c#]asp.net微信公众平台开发(3)微信消息封装及反 ...