The Cow Lexicon(dp)
Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 7290 | Accepted: 3409 |
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
Sample Input
6 10
browndcodw
cow
milk
white
black
brown
farmer
Sample Output
2 题意:给出一个由m个字符组成的单词和n个单词表,问至少删除多少个字母使这个单词才能由下面的单词表中的单词组成;
browndcodw 中删除两个d后由brown 和 cow 组成; 不得不说dp题真心难,想了半天没思路,发现自己还是太弱了,BU童鞋给讲的思路; 思路:从最后一个字母开始匹配,先初始化,dp[m] = 0,假使当前字母不能匹配,则dp[i] = dp[i+1] + 1;然后,遍历单词表中每个单词,如果有单词的首字母与当前字母相同,那么该单词有可能和这个字母以后的单词匹配,经判断后若能匹配,就算出匹配成功要删除的字母个数,我的是(cur-i)-len[j],取dp[i] 和 dp[cur]+(cur-i)-len[j] 的较小者
#include<stdio.h>
#include<string.h>
#include<algorithm>
using namespace std; char s[];//待匹配单词
char dict[][];//单词表
int dp[];// dp[i]表示到第i个字母为止要删除的最少字母数;
int len[];//保存单词表中每个单词的长度;
int n,m; int main()
{
int i,j;
scanf("%d %d",&n,&m);
scanf("%s",s);
for(i = ; i < n; i++)
{
scanf("%s",dict[i]);
len[i] = strlen(dict[i]);
} dp[m] = ;//初始化 for(i = m-; i >= ; i--)
{
dp[i] = dp[i+] + ;//初始化 for(j = ; j < n; j++)//遍历每个单词
{
if(dict[j][] == s[i] && (m-i) >= len[j])//若有个单词的首字母与s[i]相同
{
int cur = i+, cnt = ; while(cur < m && dict[j][cnt])
{
if(s[cur++] == dict[j][cnt])
cnt++;
}//检查是否可以匹配 if(cnt == len[j])//若能匹配,取较小者
dp[i] = min(dp[i],dp[cur]+(cur-i)-len[j]);//(cur-i)-len[j]表示删除的字母个数
}
}
}
printf("%d\n",dp[]);
return ;
}
The Cow Lexicon(dp)的更多相关文章
- POJ3267 The Cow Lexicon(DP+删词)
The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9041 Accepted: 4293 D ...
- 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 ...
- POJ3267 The Cow Lexicon(dp)
题目链接. 分析: dp[i]表示母串从第i位起始的后缀所对应的最少去掉字母数. dp[i] = min(dp[i+res]+res-strlen(pa[j])); 其中res 为从第 i 位开始匹配 ...
- 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 ...
- POJ 3267 The Cow Lexicon
又见面了,还是原来的配方,还是熟悉的DP....直接秒了... The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submis ...
- The Cow Lexicon
The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8815 Accepted: 4162 Descr ...
随机推荐
- xslt语法之---基础语法
1. XSLT常用元素: 1.1 <xsl:template>:创建模板 Match属性的作用是使模板和XML元素相关联 <xsl:template match=" ...
- (转)javaScript call 函数的用法说明
call 方法 请参阅 应用于:Function 对象 要求 版本 5.5 调用一个对象的一个方法,以另一个对象替换当前对象. call([thisObj[,arg1[, arg2[, [,.argN ...
- java.util.Random深入理解
java.util.Random next方法的原理 比较好的参考文档: http://isky001.iteye.com/blog/1339979 package random.utilrandom ...
- ORACLE添加作业
--创建job declare job number; beginsys.dbms_job.submit(job,'prc_into_actiwager;',sysdate,'sysdate+30/( ...
- 表单提交对chrome记住密码的影响
在处理注册.登录等含有用户名,密码的元素的表单时,chrome会主动的提示记住密码,然而这个功能在用户名的选择上真是耐人寻味,它总是寻找离password input控件最近的那一个文本框的内容,作为 ...
- iOS 去除导航栏下的黑线
Swift: navigationController?.navigationBar.setBackgroundImage(UIImage(), forBarMetrics: .Default) na ...
- cocos2dx JAVA,C++互相调用函数
C++调用JAVA 例子 #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID) #include "platform/android/jni/Jni ...
- 【vc】14_网络编程_socket编程
1.计算机网络基本知识 最简单的网络程序如图: 提示:IP地址就相当于一个公司的总机号码,端口号就相当于分机号码.在打电话时,拨通总机后,还需要转到分机上. (1)协议 ·为进行网络中的数据交换(通信 ...
- mysqldump: command not found问题解决
首先得知道mysql命令或mysqldump命令的完整路径,可以使用find命令查找除非你知道mysql安装路径可以略过这一步. find / -name mysql -print 例如我的mysql ...
- 单选按钮 点击value值自动把单选按钮选中
HTML 代码 <tr> <td align="right">性别:</td> <td><inputt ...