POJ3267 The Cow Lexicon(DP+删词)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 9041 | Accepted: 4293 |
Description
Few know that the cows have their own dictionary with W (1 ≤ W ≤ 600) words, each containing no more 25 of the characters 'a'..'z'. Their cowmunication system, based on mooing, is not very accurate; sometimes they hear words that do not make any sense. For instance, Bessie once received a message that said "browndcodw". As it turns out, the intended message was "browncow" and the two letter "d"s were noise from other parts of the barnyard.
The cows want you to help them decipher a received message (also containing only characters in the range 'a'..'z') of length L (2 ≤ L ≤ 300) characters that is a bit garbled. In particular, they know that the message has some extra letters, and they want you to determine the smallest number of letters that must be removed to make the message a sequence of words from the dictionary.
Input
Line 2: L characters (followed by a newline, of
course): the received message
Lines 3..W+2: The cows' dictionary, one
word per line
Output
of characters that need to be removed to make the message a sequence of
dictionary words.
Sample Input
6 10
browndcodw
cow
milk
white
black
brown
farmer
Sample Output
2
Source
dp[i]表示message从i位置开始需要删除几个字母
dp[i] = dp[i + 1] + 1 // 表示它在i位置并不匹配,删除i位置的字母,
dp[i] = min(dp[i],dp[i + len + t] + t) //对于长度为len的单词,从i开始需要删除t个字母才能完全匹配,i+len+t表示匹配后的下一个,dp[i+len+t]就是匹配成功下一个位置的需要删除的字母数
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cstdio>
using namespace std;
char dic[][];
char mess[ + ];
int w,l,dp[];
int DP(int x, int len, int y)
{
int j = , tot = ;
while(x <= l)
{
if(mess[x] == dic[y][j])
j++;
else
tot++;
if(j == len + ) // j==len的时候还没结束,还要匹配len位置,len是最后一个
return tot;
x++;
}
return -;
}
int main()
{
while(scanf("%d%d", &w, &l) != EOF)
{
scanf("%s", mess + );
for(int i = ; i <= w; i++)
{
scanf("%s", dic[i] + );
}
memset(dp, , sizeof(dp));
for(int i = l; i > ; i--)
{
dp[i] = dp[i + ] + ;
for(int j = ; j <= w; j++)
{
int len = strlen(dic[j] + );
int t = DP(i, len, j);
if(t != -)
{
dp[i] = min(dp[i], dp[i + len + t] + t);
}
}
}
printf("%d\n", dp[]);
}
return ;
}
POJ3267 The Cow Lexicon(DP+删词)的更多相关文章
- POJ3267 The Cow Lexicon(dp)
题目链接. 分析: dp[i]表示母串从第i位起始的后缀所对应的最少去掉字母数. dp[i] = min(dp[i+res]+res-strlen(pa[j])); 其中res 为从第 i 位开始匹配 ...
- POJ3267——The Cow Lexicon(动态规划)
The Cow Lexicon DescriptionFew know that the cows have their own dictionary with W (1 ≤ W ≤ 600) wor ...
- poj3267--The Cow Lexicon(dp:字符串组合)
The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8211 Accepted: 3864 D ...
- POJ 3267-The Cow Lexicon(DP)
The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8252 Accepted: 3888 D ...
- USACO 2007 February Silver The Cow Lexicon /// DP oj24258
题目大意: 输入w,l: w是接下来的字典内的单词个数,l为目标字符串长度 输入目标字符串 接下来w行,输入字典内的各个单词 输出目标字符串最少删除多少个字母就能变成只由字典内的单词组成的字符串 Sa ...
- The Cow Lexicon(dp)
Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 7290 Accepted: 3409 Description Few k ...
- POJ 3267:The Cow Lexicon(DP)
http://poj.org/problem?id=3267 The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submi ...
- POJ 3267:The Cow Lexicon 字符串匹配dp
The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8905 Accepted: 4228 D ...
- BZOJ 1633: [Usaco2007 Feb]The Cow Lexicon 牛的词典
题目 1633: [Usaco2007 Feb]The Cow Lexicon 牛的词典 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 401 Solv ...
随机推荐
- pl/sql developer中的SQL语句
1.无论是在查询还是在插入的时候,值都必须是单引号‘’,否则会报错,ORA-00904:标识符无效.
- project和task
projects和tasks是Gradle中最重要的两个概念 任何一个Gradle构建狗屎一个或多个projects的组成.每个project包括许多可构建组成部分 什么是 project ? 一个j ...
- Swift3.0 进制转换
Swift3.0 进制转换 模块可以直接使用,写的不是很好,欢迎来喷 // Data -> HexStrings func dataToHexStringArrayWithData(data: ...
- c语言 指针的值
int num = 1; int *p = # printf("%x", &num);//打印 0x7dfe88 printf("\n%x&quo ...
- php基础11:运算符
<?php $a = 5; $b = ++$a; echo '$a'.$a; echo "<br>"; echo '$b'.$b; echo "< ...
- JavaScript及其异步实现
由于javascript本身是单线程模型,这里主要通过Callbacks,Listeners,Control Flow Libraries ,Promises四种方式来实现异步操作. Referenc ...
- CSS 实现加载动画之四-圆点旋转
圆点旋转也是加载动画中经常用到的.其实现方式和菊花旋转一样,只不过一个是线条形式,一个是圆点形式.圆点按照固定的旋转角度排列,加上延时的改变透明度的动画就可以实现.这个实现也比较简单. 1. 动画截图 ...
- REST: C#调用REST API (zz)
由于辞职的原因,最近正在忙于找工作.在这段期间收到了一家公司的上机测试题,一共两道题,其中一道题是关于REST API的应用.虽然在面试时,我已经说过,不懂REST,但那面试PM还是给了一道这题让我做 ...
- 零散知识记录-Jira的安装
Jira不支持openjdk,在linux下需要卸载后,装个jdk才行.
- WPF Command Binding
<Window x:Class="WpfTest.MainWindow" xmlns="http://schemas.microsoft.com/winfx/200 ...