Problem 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

题意:给出一个长度为l的字符串,与w个词,问最少删掉字符串中的几个字母后可使字符串全部由所给的单词组成。

思路:

用 f[i]=n 来表示字符串删掉n个字母后剩下的串可由单词构成,易得:

若不存在单词可以与字符串str中str[i]开头的字符串匹配,则有:f[i]=f[i+1]+1

若存在单词可以匹配,且str[t-1]为可以匹配的字符串的最后一个字符,匹配的单词的长度为n,则有:f[i]=min(f[i+1]+1,f[t]+t-i-n)

Source Program

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<cstdlib>
#include<queue>
#include<set>
#include<vector>
#define INF 0x3f3f3f3f
#define PI acos(-1.0)
#define N 1001
#define MOD 123
#define E 1e-6
using namespace std;
string str;
string word[N];
int f[N];
int main()
{int w,l;scanf("%d%d",&w,&l);cin>>str;for(int i=0;i<w;i++)cin>>word[i];f[l]=0;for(int i=l-1;i>=0;i--){f[i]=f[i+1]+1;for(int j=0;j<w;j++){if(word[j].length()<=l-i && word[j].at(0)==str[i])//当第j个单词的首字母与字符串中匹配时{int t=i;int n=0;while(t<l){if(word[j].at(n)==str[t++])//匹配单词第n个字母是否与字符串匹配n++;if(n==word[j].length()){f[i]=min(f[t]+t-i-n,f[i]);break;}}}}}printf("%d\n",f[0]);
}

The Cow Lexicon(POJ-3267)相关推荐

  1. Silver Cow Party (POJ - 3268 )

    Silver Cow Party (POJ - 3268 ) 这道题是我做的最短路专题里的一道题,但我还没做这个,结果比赛就出了,真是.......... 题目: One cow from each ...

  2. 【二分】Best Cow Fences(poj 2018)

    Best Cow Fences poj 2018 题目大意: 给出一个正整数数列,要你求平均数最大,长度不小于M的字串,结果乘1000取整 输入样例 10 6 6 4 2 10 3 8 5 9 4 1 ...

  3. Bailian2734 十进制到八进制【入门】(POJ NOI0113-45)

    问题链接:POJ NOI0113-45十进制到八进制 2734:十进制到八进制 总时间限制: 1000ms 内存限制: 65536kB 描述 把一个十进制正整数转化成八进制. 输入 一行,仅含一个十进 ...

  4. Bailian2676 整数的个数【入门】(POJ NOI0105-11)

    问题链接:POJ NOI0105-11 整数的个数 2676:整数的个数 总时间限制: 1000ms 内存限制: 65536kB 描述 给定k(1 < k < 100)个正整数,其中每个数 ...

  5. Bailian4029 数字反转【进制】(POJ NOI0105-29)

    问题链接:POJ NOI0105-29 数字反转 4029:数字反转 总时间限制: 1000ms 内存限制: 65535kB 描述 给定一个整数,请将该数各个位上数字反转得到一个新数.新数也应满足整数 ...

  6. Bailian2735 八进制到十进制【入门】(POJ NOI0113-46)

    问题链接:POJ NOI0113-46 八进制到十进制 2735:八进制到十进制 总时间限制: 1000ms 内存限制: 65536kB 描述 把一个八进制正整数转化成十进制. 输入 一行,仅含一个八 ...

  7. 吴昊品游戏核心算法 Round 7 —— 熄灯游戏AI(有人性的Brute Force)(POJ 2811)

    暴力分为两种,一种属于毫无人性的暴力,一种属于有人性 的暴力.前面一种就不说了,对于后面一种情况,我们可以只对其中的部分问题进行枚举,而通过这些子问题而推导到整个的问题中.我称之为有人性的Brute ...

  8. 昂贵的聘礼(poj 1062)

    Description 年轻的探险家来到了一个印第安部落里.在那里他和酋长的女儿相爱了,于是便向酋长去求亲.酋长要他用10000个金币作为聘礼才答应把女儿嫁给他.探险家拿不出这么多金币,便请求酋长降低 ...

  9. 主席树学习小结(POJ 2104)

    在高中的时候就听到过主席树了,感觉非常高端,在寒假的时候 winter homework中有一题是查找区间第K大的树,当时就开始百度这种网上的博客,发现主席树看不懂,因为那个root[i],还有tx[ ...

  10. A - TOYS(POJ - 2318) 计算几何的一道基础题

    Calculate the number of toys that land in each bin of a partitioned toy box. 计算每一个玩具箱里面玩具的数量 Mom and ...

最新文章

  1. 使用Python,OpenCV的Meanshift 和 Camshift 算法来查找和跟踪视频中的对象
  2. PAT (Advanced Level) 1140~1143:1140模拟 1141模拟 1142暴力 1143 BST+LCA
  3. mac 完全卸载vscode
  4. SQL Sever 性能调优
  5. IntelliJ IDEA for Mac如何查看某个方法的实现
  6. Java后台与VUE跨域交接
  7. 入门科普:一文看懂NLP和中文分词算法(附代码举例)
  8. faststart可以卸载吗_电脑上的许多Microsoft Visual c++组件,可以卸载吗?
  9. C#基础1:输入输出+变量定义
  10. 智慧高校怎么做教育监控?Smartbi高校大数据服务平台来帮您
  11. 英文论文评审意见_sci英文论文审稿意见怎么写(7)
  12. 主打产品“火力不足”致使发行人持续盈利能力下降,这公司创业板IPO被终止
  13. win7系统sql连接不上服务器,Win7 安装软件时无法连接sql server解决方法
  14. 汇编bne的问题 汇编中的标号1: 以及bne 1b解释
  15. 太极计划——华夏民族软件腾飞的计划
  16. 如果推动世界进步的是魔法,世界将会怎样?
  17. modis 通道简介
  18. oracle中treat函数,PL/SQL Challenge 每日一题:2016-3-24 面向对象编程:向下转型TREAT...
  19. 2022G2电站锅炉司炉考试练习题及在线模拟考试
  20. 2021年漏洞小结-手工自检篇

热门文章

  1. 大数据平台的3个核心功能
  2. 清华大学朱旭峰:中国智库大数据报告2017预发布
  3. 我面试几乎必问:你设计索引的原则是什么?怎么避免索引失效?
  4. 今天,我收到了蚂蚁金服offer
  5. 今日头条的 ByteSpider,怎么就成了小网站的“噩梦”?
  6. 重构 - 美股行情系统APP推送改造
  7. 万字长文精华之数据中台构建五步法
  8. java getCause()与e.getMessage() 异常日志区别
  9. JeeWx 商业版本最近新增什么功能啦?
  10. ElementUI + express实现头像上传及后台图片保存