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> ]; ][], ...
随机推荐
- MySQL中case when的基本用法总结
MySQL中case when的基本用法总结原创Backcanhave7 最后发布于2018-12-06 15:14:15 阅读数 439 收藏展开MySQL中的case when有用两种用法,官方文 ...
- JAVA语言实现简单登录界面
程序设计思想: 使用Math.random()方法循环生成6个97~122之间的随机整数(对应ASCII码值‘a’~‘z’),将其转化为char型变量,连接成为一个6位字符串作为验证码输出,提示用户输 ...
- 为PHP开发搭建环境
为了能在自己的电脑上(mac OS系统)开始编写PHP代码并完成运行,需要有: 1.安装Web服务器 2.安装PHP 3.安装数据库,比如MySQL 4.一个PHP的IDE 为了上面所提到的1~3步的 ...
- [read -p应用]插拔光模块去检查port present状态
#!/bin/bash path="/sys/devices/platform/soc/fd880000.i2c-pld/i2c-0/i2c-4/i2c-15/15-0060" a ...
- MySQL(window10)加载配置文件的顺序
mysql加载配置的顺序为:(mysql --help中有详细的说明) C:\WINDOWS\my.ini C:\WINDOWS\my.cnf C:\my.ini C:\my.cnf D:***\my ...
- python爬虫(五) ProxyHandler处理器
ProxyHandler处理器 一.如果我们在一段时间内用某个ip地址访问了一个网站次数过多,网站就检测到不正常,就会禁止这个ip地址的访问.所以我们可以设置一些代理服务器,每段时间换个代理,就算ip ...
- python爬虫(四) 内涵段子
import requests import time import json from urllib import request from urllib import parse url = 'h ...
- day19-Python运维开发基础(类的魔术方法)
1. __new__魔术方法 # ### __new__ 魔术方法 ''' 触发时机:实例化类生成对象的时候触发(触发时机在__init__之前) 功能:控制对象的创建过程 参数:至少一个cls接受当 ...
- 【PAT甲级】1034 Head of a Gang (30 分)
题意: 输入两个正整数N和K(<=1000),接下来输入N行数据,每行包括两个人由三个大写字母组成的ID,以及两人通话的时间.输出团伙的个数(相互间通过电话的人数>=3),以及按照字典序输 ...
- sql数据库的基本操作
命令行 1.显示当前数据库服务器中的数据库列表:mysql> SHOW DATABASES;2.建立数据库:mysql> CREATE DATABASE 库名;3.建立数据表:mysql& ...