The Cow Lexicon
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 9380   Accepted: 4469

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 1: Two space-separated integers, respectively: W and L  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

Line 1: a single integer that is the smallest number 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

  题意:给出一个串s,还有n个子串str[n],然后求s串要删除多少个字符才能让s串匹配了str[n]里的串后没有多余字符(有点表意不清,大概能懂就好)。
 /*
dp数组的意义:从s中第i个字符开始,到尾部这段区间所删除的字符数,初始dp[len]=0;
转移方程:1) dp[i]=dp[i+1]+1 最坏情况下,每次都无法匹配
2) dp[i]=min(dp[i],dp[y]+(y-i)-l) 若有一个字符串能够匹配成功,那么
就取最优。含义:该匹配是从i到y进行匹配的,s串扫过的长度为y-i,
其中有l个字符可以匹配,因此剩下(y-i)-l个字符无法进行匹配。
*/
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <string>
#include <queue>
#include <iostream>
using namespace std; char s[],a[][];
int dp[]; int main()
{
int n,len;
cin>>n>>len;
cin>>s;
for(int i=;i<n;i++){
scanf("%s",a[i]);
}
int j,cnt;
dp[len]=;
for(int i=len-;i>=;i--){
dp[i]=dp[i+]+;
for(j=;j<n;j++){
int l=strlen(a[j]);
if(l<=len-i&&a[j][]==s[i]){
int x=,y=i;
while(y<len){
if(a[j][x]==s[y++]){
x++;
}
if(x==l){
dp[i]=min(dp[i],dp[y]+(y-i)-l);
break;
}
}
}
}
}
cout<<dp[]<<endl;
return ;
}

2016-05-29

 

POJ 3267:The Cow Lexicon(DP)的更多相关文章

  1. POJ 3267:The Cow Lexicon 字符串匹配dp

    The Cow Lexicon Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8905   Accepted: 4228 D ...

  2. poj 3267 The Cow Lexicon(dp)

    题目:http://poj.org/problem?id=3267 题意:给定一个字符串,又给n个单词,求最少删除字符串里几个字母,能匹配到n个单词里 #include <iostream> ...

  3. [luoguP2875] [USACO07FEB]牛的词汇The Cow Lexicon(DP)

    传送门 f[i] 表示前 i 个字符去掉多少个 的最优解 直接暴力DP ——代码 #include <cstdio> #include <cstring> #include & ...

  4. [USACO2002][poj1946]Cow Cycling(dp)

    Cow CyclingTime Limit: 1000MS Memory Limit: 30000KTotal Submissions: 2468 Accepted: 1378Description ...

  5. 【个人训练】The Cow Lexicon(POJ-3267)

    继续大战dp.2018年11月30日修订,更新一下现在看到这个题目的理解(ps:就现在,poj又503了). 题意分析 这条题目的大意是这样的,问一字符串内最少删去多少的字符使其由给定的若干字符串构成 ...

  6. 九度OJ 1153:括号匹配问题 (DP)

    时间限制:1 秒 内存限制:32 兆 特殊判题:否 提交:5193 解决:2248 题目描述: 在某个字符串(长度不超过100)中有左括号.右括号和大小写字母:规定(与常见的算数式子一样)任何一个左括 ...

  7. 51Nod 1049:最大子段和(dp)

    1049 最大子段和  基准时间限制:1 秒 空间限制:131072 KB 分值: 0 难度:基础题  收藏  关注 N个整数组成的序列a[1],a[2],a[3],-,a[n],求该序列如a[i]+ ...

  8. POJ - 3267 The Cow Lexicon(动态规划)

    https://vjudge.net/problem/POJ-3267 题意 给一个长度为L的字符串,以及有W个单词的词典.问最少需要从主串中删除几个字母,使其可以由词典的单词组成. 分析 状态设置很 ...

  9. 【POJ 3176】Cow Bowling(DP)

    题 Description The cows don't use actual bowling balls when they go bowling. They each take a number ...

随机推荐

  1. [ROS] slam_gmapping

    slam_gmapping节点 1)slam_gmapping 节点在sensor_msgs/LaserScan消息内获取数据并建立地图 map(nav_msgs/OccupancyGrid).该地图 ...

  2. javascript设计模式学习之十四——中介者模式

    一.中介者模式的定义和应用场景 中介者模式的作用在于解除对象之间的紧耦合关系,增加一个中介者之后,所有对象都通过中介者来通信,而不是互相引用,当一个对象发生变化的时候,仅需要通知中介者即可.从而将网状 ...

  3. C# MVC 实现登录的5种方式

    最近悟出来一个道理,在这儿分享给大家:学历代表你的过去,能力代表你的现在,学习代表你的将来. 十年河东十年河西,莫欺少年穷.  学无止境,精益求精    小弟之前做过三月的MVC,后来又一直webFo ...

  4. Python:安装mssql模块功能,并实现与sqlserver连接、查询

    由于我系统是x64系统,所以下载python2.7 x64.下载地址:https://www.python.org/downloads/release/python-2712/, 经过测试发现这个版本 ...

  5. Java基础(46):选择排序的Java封装(完整可运行)

    1 package lsg.ap.select; import java.util.Random; public class SelectSort { //选择排序 /** *@author: 梁山广 ...

  6. PHP XDEBUG

    PHP调试时,不得不提XDEBUG这个调试利器.学习PHP以来,几乎所有的问题我都利用它来解决. 首先关于如何安装,不在赘述,请自行google之.(需要特别注意的是:PHP5.2 和5.3 ,关于加 ...

  7. ViewPager相互嵌套,导致子ViewPager无法滑动,且子ViewPager中的view无法被点击

        场景:当使用ViewPager进行嵌套的时候,子viewPager是无法进行嵌套的,因此我们要重写ViewPager类,并重写里层viewPager类中的onTouchEvent方法,调用其父 ...

  8. JAVA实现File类中的遍历操作并输出内容

    package shb.java.testIo; import java.io.BufferedReader; import java.io.BufferedWriter; import java.i ...

  9. Struts2部署在Websphere上的问题

    配置Mapped Filter:可以解决Struts2的项目部署在WebSphere6.1下面,发生Action找不到的情况. 应用程序服务器>[选择所使用的服务器]>Web 容器设置&g ...

  10. 夺命雷公狗—angularjs—18—angularjs的事件

    对于一款前端框架,提起事件,很容易让人联想到DOM事件,比如说鼠标点击以及页面滚动等.但是我们这里说的angular中的事件和DOM事件并不是一个东西. 事件的发布 我们可以通过 $emit() 以及 ...