POJ 3267 The Cow Lexicon(DP)
来源:互联网 发布:知乎 新浪微博煮肘是谁 编辑:程序博客网 时间:2024/04/27 16:04
题意:给出一个字符串和一个字典,求出最少删除这个字符串中的几个字符使得字符串是字典中单词的一个组合。
思路:DP。用dp[i]表示以i为后缀的字符串中最少删除几个字符使得字符串是字典中单词的一个组合,那么我们就可以得到状态转移方程dp[i] = min(dp[i], dp[pos+1]+pos-i+1-strlen(word)),初始化dp[i] = dp[i+1] + 1。
#include<cstdio>#include<cstring>#include<cmath>#include<cstdlib>#include<iostream>#include<algorithm>#include<vector>#include<map>#include<queue>#include<stack>#include<string>#include<map>#include<set>#include<ctime>#define eps 1e-6#define LL long long#define pii pair<int, int>//#pragma comment(linker, "/STACK:1024000000,1024000000")using namespace std;const int MAXN = 350; int dp[MAXN], n, m, len[700];char str[700][50], T[MAXN];int main() { //freopen("input.txt", "r", stdin);while(scanf("%d", &m) == 1) {scanf("%d", &n);cin >> T+1;for(int i = 1; i <= m; i++) {scanf("%s", str[i]+1);len[i] = strlen(str[i]+1);}dp[n+1] = 0;for(int i = n; i; i--) {dp[i] = dp[i+1] + 1; for(int j = 1; j <= m; j++) {if(str[j][1] != T[i]) continue;int pos = i;for(int k = 1; k<=len[j] && pos <= n; k++) {while(pos<=n && T[pos]!=str[j][k]) pos++;if(pos<=n && T[pos]==str[j][k]) {if(k == len[j]) dp[i] = min(dp[i], dp[pos+1]+pos-i+1-len[j]);pos++;}}}}//for(int i = 1; i <= n; i++) cout << dp[i] << endl;cout << dp[1] << endl;} return 0;}
0 0
- 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 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 3267 The Cow Lexicon dp
- POJ 3267 The Cow Lexicon (简单DP)
- POJ-3267-The Cow Lexicon-DP
- poj-3267 The Cow Lexicon【dp】
- 九度OJ 1027:欧拉回路 (欧拉回路)
- JAVA-反射-GETGENERICSUPERCLASS()介绍
- yum源出现问题
- Hibernate运行时遇到的错误小结
- struts-helloworld
- POJ 3267 The Cow Lexicon(DP)
- 专家教诲:变频器进行多电机启动时的切换要点
- SPFA+Stack||Dijkstra+Heap-POJ-3159-Candies
- 各个JSON技术的比较
- 解读RunTime运行时机制
- 从校园到工作的路(二)
- 五步搞定Android开发环境部署——非常详细的Android开发环境搭建教程
- 关于树的练习(一):
- 黑马程序员——【学习笔记】反射