POJ-3267(DP)
来源:互联网 发布:上海拉面馆事件知乎 编辑:程序博客网 时间:2024/06/05 16:05
关键是状态定义和状态转换,看了discuss才写出来的,唉,水平还是不到呢:
令dp[i]表示message[i,L)需要删除几个字符才能用字典中的单词表示,则dp[i] = min(dp[i], dp[m]+(m-i)-word.size()),其中m是指如果用一个word在i处进行匹配的话,匹配完全这个word后的下标位置
#include <iostream>#include <string>#include <algorithm>using namespace std;int W, L;string word[600], msg;int dp[301];int main(){ ios::sync_with_stdio(false); while(cin >> W){ cin >> L >> msg; for(int i = 0; i < W; ++i) cin >> word[i]; dp[L] = 0; for(int i = L-1; i >= 0; --i){ dp[i] = dp[i+1] + 1;//try to delete msg[i] for(int j = 0; j < W; ++j){ const string& s = word[j]; int len = s.size(); if(i + len <= L && s[0] == msg[i]){//try to reserve msg[i] int m = i + 1, n = 1; while(m < L && n < len){ if(msg[m] == s[n]) ++m, ++n; else ++m; } //注意这里n如果不等于len,则说明在s中有[i,L)区间内没有对应出现的字符,即不能用s在i出进行匹配 if(n == len) dp[i] = min(dp[i], dp[m] + (m - i) - len);//can use word[j] to match msg[i,m) } } } cout << dp[0] << "\n"; } return 0;}
0 0
- poj 3267(dp)
- POJ-3267(DP)
- POJ 3267 - The Cow Lexicon (dp)
- poj 3267 The Cow Lexicon (dp)
- POJ 3267 The Cow Lexicon(DP)
- POJ 3267 The Cow Lexicon(DP)
- POJ 3267 The Cow Lexicon(DP)
- POJ 3267The Cow Lexicon(DP)
- poj 3267 The Cow Lexicon (DP)
- POJ 3267 The Cow Lexicon (dp)
- POJ 3267 The Cow Lexicon (DP)
- POJ 3267 (DP)
- poj 3267 简单dp
- POJ 3267 DP
- poj 3267 dp
- poj 1160(dp)
- Poj 2229(dp)
- Poj 2385 (dp)
- ios中的音频播放,好文,mark,等待后续
- 173VPN带来如何解决打不开路由器方案
- 防盗锁发送到发送到规范的随碟附送都飞放到
- IOS7 开发注意事项
- destoon实现调用热门关键字的方法
- POJ-3267(DP)
- USACO section1.2 Dual Palindromes
- REUSE_ALV_GRID_DISPLAY 实现ALV显示
- Socket通信原理和实践
- c++ 银行管理系统及报告
- work note
- django installed on APPS and ...
- 分段函数求值1
- layout_weight