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> ]; ][], ...
随机推荐
- php 算法知识 猴子选大王
一群猴子排成一圈,按1,2,...,n依次编号. 然后从第1只开始数,数到第m只,把它踢出圈, 从它后面再开始数,再数到第m只,在把它踢出去..., 如此不停的进行下去,直到最后只剩下一只猴子为止,那 ...
- python3.6.5修改print的颜色
开头部分:\033[显示方式;前景色;背景色m +想要输出的内容:\033[0m 注意:开头部分的三个参数:显示方式,前景色,背景色是可选参数,具体参数效果见下文,可以只写其中的某一个:参数 ...
- Python - 代码片段,Snippets,Gist
说明 代码片段来自网上搬运的或者自己写的 华氏温度转摄氏温度 f = float(input('请输入华氏温度: ')) c = (f - 32) / 1.8 print('%.1f华氏度 = %.1 ...
- Java 笔试题
有一些还是存在问题,欢迎大家一起探讨. 在Java类中,使用以下( )声明语句来定义公有的int型常量MAX. A. public int MAX = 100; B. final int MAX = ...
- 关于TXT文件中英文单词出现频率排序问题
题目要求: 指定文件目录, 但是会递归遍历目录下的所有子目录,输出文件中所有不重复的单词,按照出现次数由多到少排列. 源码: package word; import java.io.File; i ...
- minst.npz下载
keras.datasets.mnist数据集下载地址 下载地址:链接: https://pan.baidu.com/s/1Rr-aHsIIEQx2z6W3qvMmhQ 提取码: 8w15
- windows下svn post-commit的实现
前言: 好的!在结束了上一博客教程的Subversion安装之后.我们开始了下一项工作,windows版本下 svn post-commit的实现.说实话,这方面的知识网上的知识并不是很多~~~~~~ ...
- highcharts Ajax 动态请求加载数据
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"% ...
- DRF项目之序列化器和视图重写方法的区别
我们,都知道,DRF框架是一款高度封装的框架. 我们可以通过重写一些方法来实现自定义的功能. 今天,就来说说在视图中重写和序列化器中重写方法的区别. 在视图中重写方法: 接收请求,处理数据(业务逻辑) ...
- C语言中的结构体是怎么定义的_怎么使用?
结构体的定义 // 定义结构体st struct st{ int a; // 成员a int b; // 成员b }; #include <stdio.h> struct st{ int ...