poj3267
来源:互联网 发布:网络大电影立项新规定 编辑:程序博客网 时间:2024/05/21 07:03
题意:给一个字符串,和一些单词,问最少去掉几个字母才能使文章都由那些单词组成。
思路:用动态规划来解决,去掉的字母等于总的长度减去匹配好的字母数。
因为匹配数比较好理解,所以用匹配数来解决的问题。
f( i )表示到第i个位置为止的最大匹配数,因为位置增加1的时候最多增加一个匹配的单词,
所以每次只检查一下每个位置匹配每个单词能给给匹配数带来的增量,
每次找到匹配字母增加数最大的就是这个位置为止的最优解。
动态规划式:
当该单词无法匹配的话:f( i ) = f(i - 1)
如果可以匹配的话f( i ) = max(f( t ) + 该单词的长度,f(i - 1))
(t表示字符串里对应匹配对应单词的前一个位置)
这道题特别应该注意的是字符串匹配的时候,一定要注意检查指针的移动。
刚开始就是因为指针移动的地方马虎贡献了一次WA。
#include<iostream> #include<Cstdio>#include<string>using namespace std; int dp[1000], length, size, i, j, k;char word[1000];char dic[800][30];int dicLength[800];int match(int pos, int num){int temp2 = pos, i, length1;char t1 = word[pos], t2 = dic[num][dicLength[num] - 1]; if(t1 != t2)return -1;for(i = dicLength[num] - 1; i >= 0; i--){t2 = dic[num][i];while(temp2 >= 0 && word[temp2] != t2){if(word[temp2] == t2)break;temp2--;}if(temp2 == -1)return -1;if(i != 0)temp2--;}return temp2;}int main() { char *p; int max, temp, temp1;scanf("%d%d", &size, &length);scanf("%s", word);p = word;for(i = 0; i < size; i++){scanf("%s", dic[i]);p = dic[i]; dicLength[i] = 0;while(*p != '\0'){p++;dicLength[i]++;}}dp[0] = 0;for(i = 1; i < length; i++){max = dp[i - 1];for(j =0; j < size; j++){temp = match(i, j);if(temp > -1){//cout << temp << endl;if(temp == 0 && dicLength[j] > max)max = dicLength[j];else if(dicLength[j] + dp[temp - 1] > max)max = dicLength[j] + dp[temp - 1];}}dp[i] = max;}cout << length - dp[length - 1] << endl;return 0;}
0 0
- POJ3267
- poj3267
- poj3267
- poj3267
- poj3267
- poj3267
- POJ3267
- poj3267
- poj3267
- poj3267
- [DP] poj3267
- poj3267 dp
- poj3267 dp
- POJ3267 DP
- POJ3267 The Cow Lexicon
- POJ3267--The Cow Lexicon
- poj3267 The Cow Lexicon
- POJ3267 线性DP
- 微软帮助自带的弹出文件浏览对话框SHBrowseForFolder
- 查询所有表 表的所有列 与表相关的所有视图、存储过程、函数
- Merge Intervals
- 互斥量与信号量
- Java Socket 网络编程常见异常
- poj3267
- 常用git资源
- JDBC存储过程
- 使用ViewPager实现高仿launcher拖动效果
- 5月30日,今天是5月最后一个交易日,5月月线收阴已成定局,则为6月的下跌奠定了基础。上周震荡,本周黄金向下突破都是黄金6月下跌的月度需求,下周非农也难以阻止6月的空头之势,而从目前的盘面看,下周的非
- leetcode:String to Integer (atoi)
- 配置多个git远程仓库的ssh-Key切换
- android网络编程——使用Android中的网络连接
- s:iterator使用实例