POJ 3267.The Cow Lexicon
来源:互联网 发布:软件的定义 编辑:程序博客网 时间:2024/06/17 14:59
题目:http://poj.org/problem?id=3267
AC代码(C++):
#include <iostream>#include <algorithm>#include <stdio.h>#include <vector>#include <queue>#include <math.h>#include <string>#include <string.h>#include <bitset>#define INF 0xfffffff#define MAXN 100105using namespace std;int W,L;char msg[305];int dp[305];string dict[605];int main(){cin>>W>>L;cin>>msg;for(int i = 0; i < W; i++)cin>>dict[i];dp[L] = 0;int pm,pd,len;for(int i = L-1; i >= 0; i--){dp[i] = dp[i+1]+1;for(int j = 0; j < W; j++){len = dict[j].length();if(L-i>=len&&dict[j][0]==msg[i]){pm = i;pd = 0;while(pm<L){if(msg[pm]==dict[j][pd])pd++;pm++;if(pd==len){dp[i] = min(dp[i],dp[pm]+(pm-i)-len);break;}}}}}cout<<dp[0];}总结: 动态规划(E[j]=opt{D+w(i,j)}). dp[i]表示从message的第i位到末端最少去掉多少字符才能与字典匹配. 初始化dp[L]=0, 则答案就是dp[0]. i从L-1到0更新dp数组, 首先令dp[i]=dp[i+1]+1表示为最差的情况, 然后遍历字典, 看从i开始往后能否匹配到单词, 若能则更新dp数组, 使dp[i] = min(dp[i],dp[pm]+(pm-i)-len), 即取最小, dp[pm]+(pm-i)-len的意思是msg中匹配的末端位置的最小需要去掉的字符数加上跟这个词匹配所需要去掉的字符数. 更新完dp数组后dp[0]就是答案.
阅读全文
0 0
- POJ 3267.The Cow Lexicon
- The Cow Lexicon POJ
- poj 3267 The Cow Lexicon
- POJ 3267 The Cow Lexicon
- poj 3267 The Cow Lexicon
- poj 3276 The Cow Lexicon
- POJ 3267 The Cow Lexicon
- poj 3267 The Cow Lexicon
- POJ 3267 The Cow Lexicon
- POJ 3267 The Cow Lexicon
- poj-3267-The Cow Lexicon
- POJ 3267 The Cow Lexicon
- poj 3276 the cow lexicon
- POJ 3267 The Cow Lexicon
- POJ 3267 The Cow Lexicon
- POJ - 3267 The Cow Lexicon
- poj-3267-The Cow Lexicon
- POJ 3267 The Cow Lexicon
- 如何在Android模拟器中安装应用
- javascript实现json的序列化和反序列化功能
- JSONObject、JSONArray 介绍
- 截取特殊字符中间的字符串
- fdisk非交互式分区方法
- POJ 3267.The Cow Lexicon
- C语言实验——打印菱形
- juint知识点
- php设计模式
- spring线程池配置及使用
- bootstrap table的父子表数据动态加载
- 【微服务架构】SpringCloud之Ribbon(四)
- hud4768(Flyer)详解,二分搜索
- Android 动画之 ---- Drawable Animation (Frame动画,帧动画)