poj 3267 The Cow Lexicon
来源:互联网 发布:添加URL端口 编辑:程序博客网 时间:2024/06/16 13:56
Description
Few know that the cows have their own dictionary with W (1 ≤ W ≤ 600) words, each containing no more 25 of the characters 'a'..'z'. Their cowmunication system, based on mooing, is not very accurate; sometimes they hear words that do not make any sense. For instance, Bessie once received a message that said "browndcodw". As it turns out, the intended message was "browncow" and the two letter "d"s were noise from other parts of the barnyard.
The cows want you to help them decipher a received message (also containing only characters in the range 'a'..'z') of length L (2 ≤ L ≤ 300) characters that is a bit garbled. In particular, they know that the message has some extra letters, and they want you to determine the smallest number of letters that must be removed to make the message a sequence of words from the dictionary.
Input
Line 2: L characters (followed by a newline, of course): the received message
Lines 3..W+2: The cows' dictionary, one word per line
Output
Sample Input
6 10browndcodwcowmilkwhiteblackbrownfarmer
Sample Output
2
Source
![鄙视](http://static.blog.csdn.net/xheditor/xheditor_emot/default/despise.gif)
#include <stdio.h>#include <string.h>#include <algorithm>using namespace std;int dp[350];char a[350];char ch[1020][350];int len[1020];int main(){ int n,m,i,j; int ss; int t; memset(dp,0,sizeof(dp)); scanf("%d %d",&n,&m); scanf("%s",a); for(i = 0;i < n; i++) { scanf("%s",ch[i]); getchar(); len[i] = strlen(ch[i]); } int L = strlen(a); for(i = L-1;i >= 0; i--) { dp[i] = dp[i+1]+1; //每次从最差的境况入手 for(j = 0;j < n; j++) { if(ch[j][0] == a[i]&&L-i >= len[j]) //当你可以匹配时 { ss = i; t = 0; while(ss < L) { if(ch[j][t] == a[ss]) { t++; } ss++; if(t == len[j]) //只有这样才能更新数组 { dp[i] = min(dp[i],dp[ss]+ss-i-len[j]); //就是上面我说的最后一句 //(ss-i)是匹配的单句长度,ss-i-len[j],是要删除的字符数,当然不要忘了加上之前的dp[ss] } } } } } printf("%d\n",dp[0]);//结果}
代码菜鸟,如有错误,请多包涵!!
- 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 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-3267-The Cow Lexicon
- POJ 3267 The Cow Lexicon
- POJ 3267--The Cow Lexicon
- POJ 3267 The Cow Lexicon
- 【POJ 3267】 The Cow Lexicon
- The Cow Lexicon(POJ--3267
- POJ3267——The Cow Lexicon
- 205.leetcode Isomorphic Strings (easy)[map结构 字符串处理]
- 请问在两个程序之间如何通信?
- Django 模板里赋值
- 【Android】【优化】
- poj 3267 The Cow Lexicon
- 《Motion Design for iOS》(三十八)
- Jquery memo
- mail 发送邮件
- linux进程通信之pipe
- 深入浅出HashMap原理
- 'NoneType' object is not iterable
- Python 正则表达式(3)匹配手机号
- UI高级---->多线程总结