题目链接: http://poj.org/problem?id=3267

从后往前遍历,dp[i]表示第i个字符到最后一个字符删除的字符个数。

状态转移方程为:

dp[i] = dp[i+1] + 1;                                                 //当不能匹配时

dp[i] = std::min(dp[i], dp[msg] + (msg-i) - len[j]);  //当匹配时。

第i个字符到第msg个字符之间一共有msg-i个字符,减去字典中单词的长度,就是删除的字符数量。

 #include <stdio.h>
#include <string.h>
#include <algorithm>
int n, m, dp[], len[];
char s[], dic[][];
int main()
{
while(scanf("%d %d", &n, &m) != EOF)
{
scanf("%s", s);
for(int i = ; i < n; i++)
{
scanf("%s", dic[i]);
len[i] = strlen(dic[i]);
}
dp[m] = ;
for(int i = m-; i >= ; i--)
{
dp[i] = dp[i+] + ;
for(int j = ; j < n; j++)
{
if(s[i] == dic[j][] && m-i >= len[j])
{
int msg = i+, cnt = ;
while(msg < m && dic[j][cnt])
{
if(s[msg++] == dic[j][cnt])
cnt++;
}
if(cnt == len[j])
dp[i] = std::min(dp[i], dp[msg] + (msg-i) - len[j]);
}
}
}
printf("%d\n", dp[]);
}
return ;
}

POJ 3267 The Cow Lexicon 简单DP的更多相关文章

  1. poj 3267 The Cow Lexicon(dp)

    题目:http://poj.org/problem?id=3267 题意:给定一个字符串,又给n个单词,求最少删除字符串里几个字母,能匹配到n个单词里 #include <iostream> ...

  2. POJ 3267 The Cow Lexicon

    又见面了,还是原来的配方,还是熟悉的DP....直接秒了... The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submis ...

  3. poj 3267 The Cow Lexicon (动态规划)

    The Cow Lexicon Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8167   Accepted: 3845 D ...

  4. POJ - 3267 The Cow Lexicon(动态规划)

    https://vjudge.net/problem/POJ-3267 题意 给一个长度为L的字符串,以及有W个单词的词典.问最少需要从主串中删除几个字母,使其可以由词典的单词组成. 分析 状态设置很 ...

  5. POJ 3267:The Cow Lexicon(DP)

    http://poj.org/problem?id=3267 The Cow Lexicon Time Limit: 2000MS   Memory Limit: 65536K Total Submi ...

  6. POJ 3267为什么优先队列超时,DP就能过,难过

    The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 11846 Accepted: 5693 Desc ...

  7. poj 1050 To the Max (简单dp)

    题目链接:http://poj.org/problem?id=1050 #include<cstdio> #include<cstring> #include<iostr ...

  8. 【POJ 3176】Cow Bowling(DP)

    题 Description The cows don't use actual bowling balls when they go bowling. They each take a number ...

  9. PKU 3267 The Cow Lexicon(动态规划)

    题目大意:给定一个字符串和一本字典,问至少需要删除多少个字符才能匹配到字典中的单词序列.PS:是单词序列,而不是一个单词 思路:                                     ...

随机推荐

  1. Centos6下yum安装MariaDB5.5(转)

    原文地址:http://www.cnblogs.com/kgdxpr/p/3209009.html vi /etc/yum.repos.d/MariaDB.repo 加入下面内容 [mariabd]n ...

  2. mysql 索引优化

    http://blog.jobbole.com/87107/ http://www.phpben.com/?post=74 http://blogread.cn/it/article/4088?f=s ...

  3. oracle学习----特殊的连接方式

    1.笛卡儿积 merge join cartesion SQL> select ename,dname from emp,dept; 已选择56行. 执行计划------------------ ...

  4. Linux动态查看网络流量iptraf

    iptraf工具可以动态查看网络流量 yum install iptraf 查看所有网卡流量 iptraf -g

  5. Oracle错误代码案例总结及解决方案

    引自:http://blog.sina.com.cn/s/blog_9daa54ec0100yr7v.html 常见错误: ORA-00001:违反唯一约束条件(主键错误) ORA-00028:无法连 ...

  6. 20151209jquery学习笔记Ajax 代码备份

    /*$(function () { $("input").click(function() { $.ajax({ type:'POST', url:'test.php', data ...

  7. PHP和MYSQL的编码问题

    http://blog.csdn.net/martinkro/article/details/5352474 1 MYSQL中的字符集概念  Mysql的字符集里有两个概念,一个是"Char ...

  8. 学习笔记5_Day09_网站访问量统计小练习

    练习:访问量统计 一个项目中所有的资源被访问都要对访问量进行累加! 创建一个int类型的变量,用来保存访问量,然后把它保存到ServletContext的域中,这样可以保存所有的Servlet都可以访 ...

  9. ASP和PHP限制IP访问 只允许指定IP访问 允许*号通配符过滤IP

    /** * 检测访问的ip是否为规定的允许的ip * Enter description here ... */ function check_ip(){ $ALLOWED_IP=array('192 ...

  10. [firefly]暗黑源码解析

    一.架构 二.各模块详解 1.net 2.gate 3.game 4.db 三.启动 四.举例说明 五.性能测试