The Cow Lexicon
time limit per test 1 second memory limit per test 256 megabytes
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.

传送门:POJ3267

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

1
2
3
4
5
6
7
8
6 10
browndcodw
cow
milk
white
black
brown
farmer

Sample Output

1
2

题解

不错的一道题 dp[i]表示前i位需要去掉的最少数目 先正着想当j>i时dp[i]怎么迁移到dp[j]那? 看i+1到j中间的字符串和已有的字符串组比较要删除的最少数目 写代码时从后往前推 典型的区间dp模型
## AC code:(不包含输入类)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.io.*;  
import java.util.*;
public class Main {

public static void main(String[] args) {
FastScanner sc=new FastScanner();
PrintWriter pw=new PrintWriter(System.out);
while(sc.hasNext()){
int w=sc.nextInt();
int l=sc.nextInt();
String s=sc.next();
String[]word=new String[w];
for(int i=0;i<w;i++){
word[i]=sc.next();
}
int[]dp=new int[l+1];
dp[0]=0;
for(int i=1;i<=l;i++){
dp[i]=dp[i-1]+1;//init
for(int j=0;j<w;j++){
int end=i-1;
int len=word[j].length()-1;
while(end>=0&&len>=0){
if(s.charAt(end)==word[j].charAt(len)){
len--;
}
end--;//important
}
if(len==-1){//complete
dp[i]=Math.min(dp[i], dp[end+1]+(i-(end+1))-word[j].length());
}
}
}
pw.println(dp[l]);
pw.flush();
}

}

}