POJ 3267:The Cow Lexicon 字符串匹配dp
| Time Limit: 2000MS | Memory Limit: 65536K | |
| Total Submissions: 8905 | Accepted: 4228 |
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
题意是给出一个主串,给出一系列的字典单词,问从主串中最少删除多少个字符,使得主串都是有字典里的单词组成的。
自己感觉这个dp和暴力也差不太多了。
dp[x]表示从主串第一个字符到主串第x-1个字符要删除的数量。然后对于主串中的每一个位置,都对字典中的单词向前匹配,如果位置j 到位置i 匹配单词k成功,就比较一下当前的值 与i-j-len[k]+dp[j]的值。取其最小值。
感觉是一个很好理解的dp,但真做反正我没想到。。。
代码:
#include <iostream>
#include <algorithm>
#include <cmath>
#include <vector>
#include <string>
#include <cstring>
#pragma warning(disable:4996)
using namespace std; int W, L;
string message;
string word[605];
int len[605];
int dp[305]; int main()
{
int i, j, k;
cin >> W >> L;
cin >> message; for (i = 1; i <= W; i++)
{
cin >> word[i];
len[i] = word[i].length();
}
for (i = 0; i < L; i++)
{
if (i == 0)
dp[0] = 1;
else
dp[i] = dp[i - 1] + 1; for (k = 1; k <= W; k++)
{
int subs = len[k] - 1;
int temp = i;
if (subs > i)
continue;
while (subs >= 0 && temp >= 0 && temp>=subs)
{
if (message[temp] == word[k][subs])
subs--;
temp--;
}
if (subs < 0)
{
dp[i] = min(dp[i],i-temp-len[k]+dp[temp]);
}
}
}
cout << dp[i - 1] << endl;
return 0;
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
POJ 3267:The Cow Lexicon 字符串匹配dp的更多相关文章
- poj 3267 The Cow Lexicon (动态规划)
The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8167 Accepted: 3845 D ...
- POJ 3267 The Cow Lexicon
又见面了,还是原来的配方,还是熟悉的DP....直接秒了... The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submis ...
- POJ 3267 The Cow Lexicon 简单DP
题目链接: http://poj.org/problem?id=3267 从后往前遍历,dp[i]表示第i个字符到最后一个字符删除的字符个数. 状态转移方程为: dp[i] = dp[i+1] + 1 ...
- poj 3267 The Cow Lexicon(dp)
题目:http://poj.org/problem?id=3267 题意:给定一个字符串,又给n个单词,求最少删除字符串里几个字母,能匹配到n个单词里 #include <iostream> ...
- POJ - 3267 The Cow Lexicon(动态规划)
https://vjudge.net/problem/POJ-3267 题意 给一个长度为L的字符串,以及有W个单词的词典.问最少需要从主串中删除几个字母,使其可以由词典的单词组成. 分析 状态设置很 ...
- PKU 3267 The Cow Lexicon(动态规划)
题目大意:给定一个字符串和一本字典,问至少需要删除多少个字符才能匹配到字典中的单词序列.PS:是单词序列,而不是一个单词 思路: ...
- Poj 2018 Best Cow Fences(分数规划+DP&&斜率优化)
Best Cow Fences Time Limit: 1000MS Memory Limit: 30000K Description Farmer John's farm consists of a ...
- 字符串匹配dp+bitset,滚动数组优化——hdu5745(经典)
bitset的经典优化,即把可行性01数组的转移代价降低 bitset的适用情况,当内层状态只和外层状态的上一个状态相关,并且内层状态的相关距离是一个固定的数,可用bitset,换言之,能用滚动数组是 ...
- POJ 1035 Spell checker 简单字符串匹配
在输入的单词中删除或替换或插入一个字符,看是否在字典中.直接暴力,172ms.. #include <stdio.h> #include <string.h> ]; ][], ...
随机推荐
- LinkList(链表)
code: #include <stdio.h> #include <time.h> #include <conio.h> #include <stdlib. ...
- 于elasticsearch-rest-high-level-client 操作 Es
安装Java:要求JDK为1.8及以上版本. 创建阿里云Elasticsearch实例:实例版本要求大于等于elasticsearch-rest-high-level-client的版本.本文创建一个 ...
- 大数据篇:MapReduce
MapReduce MapReduce是什么? MapReduce源自于Google发表于2004年12月的MapReduce论文,是面向大数据并行处理的计算模型.框架和平台,而Hadoop MapR ...
- 解决xpath中文乱码
利用xpath建标签树以后,虽然提高了元素匹配效率,但是etree会把中文转为ASCII码,所以简单地tostring以后会有乱码. 解决方法: import requests from reques ...
- PAT甲级2019冬季考试题解
A Good In C纯模拟题,用string数组读入数据,注意单词数量的判断 #include<bits/stdc++.h> using namespace std; ; ][]; in ...
- vs2010 c++中内嵌汇编代码
在研究汇编时,需要自己写点汇编代码测试,用Ollydbg写每次加载程序就没了,不是很方便. 可以考虑直接在程序中写入汇编代码,只需要加上关键字“_asm”宏(C++代码中). 如下示例 编写环境 :v ...
- Python环境搭建-3 Python下载
python环境搭建 Python是一个跨平台.可移植的编程语言,因此可在windows.Linux和Mac OS X系统中安装使用. 安装完成后,你会得到Python解释器环境,可以通过终端输入py ...
- MPAndroidChart柱子上的文字的颜色dataSet.setValueTextColors
版本:MPAndroidChart v3.1.0 这是个很强大的图表,不同的版本对应的API会不一样. 需求描述: 用了柱状图,但要实现这样的功能,通过不同的门店来区分不同的柱子的颜色,并且柱子上文字 ...
- 提高unigui开发效率的两个方法(02)
1.编译时自己退出运行的程序. 在做unigui开发时,每次编译运行时,unigui的应用都会在后台运行,每次重新编译时都必须手工在任务栏里将应用退出才行,非常麻烦,可以在项目编译的参数里加上杀进程的 ...
- ecshop代码分析一(init.php文件)
ecshop代码分析一(init.php文件) 因为工作原因,需要对ecshop二次开发,顺便记录一下对ecshop源代码的一些分析: 首先是init.php文件,这个文件在ecshop每个页面都 ...