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> ]; ][], ...
随机推荐
- 安装 Python 虚拟环境 (Linux)
我的 Ubuntu 18.04 预安装了 python 3.6,但是没有安装 pip,所以先进行安装: apt-get install python-pip 1. 安装虚拟环境所需包: pip ins ...
- SpringBoot 集成JUnit
项目太大,不好直接测整个项目,一般都是切割成多个单元,单独测试,即单元测试. 直接在原项目上测试,会把项目改得乱七八糟的,一般是单独写测试代码. 进行单元测试,这就需要集成JUnit. (1)在pom ...
- windows破解wifi小技巧
1,首先使用手机某软件破解wifi,手机连上破解的wifi 2,在手机上打开下面界面 3,在电脑上使用二维码识别小工具扫描二维码 4,得到扫面结果 4,得到扫描结果 WIFI:T:WPA;S:DFZJ ...
- ImageMagick PDF到JPG有时会导致黑色背景
convert -verbose -density 300 -quality 50 -background white -alpha remove 0.pdf 0.jpg magick convert ...
- 【代码总结】PHP文件的上传和下载
===================== 文件上传和下载 ===================== 一.php.ini的配置信息 file_uploads = On /Off 是否允许文件上 ...
- 解决HTML5(富文本内容)连续数字、字母不自动换行
最近开发了一个与富文本相关的功能,大概描述一下:通过富文本编辑器添加的内容,通过input展示出来(这里用到了 Vue 的 v-html 指令). 也是巧合,编辑了一个只有数字组成的长文本,等到展示的 ...
- 学习笔记(12)- chatopera的语义理解系统
参考https://github.com/chatopera/clause-py-demo 前提:安装完毕 Docker.Docker Compose 和 Python3.x git clone ht ...
- 02.Scala高级特性:第6节 高阶函数;第7节 隐式转换和隐式参数
Scala高级特性 1. 课程目标 1.1. 目标一:深入理解高阶函数 1.2. 目标二:深入理解隐式转换 2. 高阶函数 2.1. 概念 Scala混合了面向对象和函数式的特 ...
- MongoDB基础篇2:数据库/用户/数据集合的增删改
一.数据库操作 创建并进入数据库: 命令:use DATABASE_NAME 示例:use tms 查看所有数据库: 命令:show dbs 注意: (1)新创建的数据库在show dbs命令 ...
- Java连载80-数字类格式、随机数、BigDecimal
一.数字类 1.关于数字格式化:java.text.DecimalFormat; 2.数字格式元素: # 任意数字 , 千分位 . 小数点 0 不够补零 package com.bjpowernode ...