POJ 3267:The Cow Lexicon(DP)

The Cow Lexicon
Time Limit: 2000MS   Memory Limit: 65536K
Total Submissions: 9380   Accepted: 4469

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 1: Two space-separated integers, respectively: W and L  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

Line 1: a single integer that is the smallest number of characters that need to be removed to make the message a sequence of dictionary words.

Sample Input

6 10
browndcodw
cow
milk
white
black
brown
farmer

Sample Output

2

  题意:给出一个串s,还有n个子串str[n],然后求s串要删除多少个字符才能让s串匹配了str[n]里的串后没有多余字符(有点表意不清,大概能懂就好)。
 1 /*
 2 dp数组的意义:从s中第i个字符开始,到尾部这段区间所删除的字符数,初始dp[len]=0;
 3 转移方程:1) dp[i]=dp[i+1]+1 最坏情况下,每次都无法匹配
 4           2) dp[i]=min(dp[i],dp[y]+(y-i)-l) 若有一个字符串能够匹配成功,那么
 5              就取最优。含义:该匹配是从i到y进行匹配的,s串扫过的长度为y-i,
 6              其中有l个字符可以匹配,因此剩下(y-i)-l个字符无法进行匹配。
 7 */
 8 #include <cstdio>
 9 #include <cstring>
10 #include <cmath>
11 #include <algorithm>
12 #include <string>
13 #include <queue>
14 #include <iostream>
15 using namespace std;
16 
17 char s[305],a[605][30];
18 int dp[305];
19 
20 int main()
21 {
22     int n,len;
23     cin>>n>>len;
24     cin>>s;
25     for(int i=0;i<n;i++){
26         scanf("%s",a[i]);
27     }
28     int j,cnt;
29     dp[len]=0;
30     for(int i=len-1;i>=0;i--){
31         dp[i]=dp[i+1]+1;
32         for(j=0;j<n;j++){
33             int l=strlen(a[j]);
34             if(l<=len-i&&a[j][0]==s[i]){
35                 int x=0,y=i;
36                 while(y<len){
37                     if(a[j][x]==s[y++]){
38                         x++;
39                     }
40                     if(x==l){
41                         dp[i]=min(dp[i],dp[y]+(y-i)-l);
42                         break;
43                     }
44                 }
45             }
46         }
47     }
48     cout<<dp[0]<<endl;
49     return 0;
50 }

2016-05-29

 
原文地址:https://www.cnblogs.com/fightfordream/p/5558691.html