POJ 3267 java实现
import java.util.Scanner;
public class Main3267 {
//poj3267
static int W, L;
static char[] message;
static int[] dp;
static int Min(int a, int b) {
if (a > b) {
return b;
} else {
return a;
}
}
public static void main(String[] args) throws Exception {
System.setIn(new java.io.FileInputStream("2.txt"));
Scanner sc = new Scanner(System.in);
while (sc.hasNext()) {
W = sc.nextInt();
L = sc.nextInt();
message = sc.next().toCharArray();
char[][] dic = new char[W][25];
for (int i = 0; i < dic.length; i++) {
dic[i] = sc.next().toCharArray();
}
dp = new int[301];
dp[L] = 0; // 初始化为0
int pivot, index; // 迭代器
for (int i = L - 1; i >= 0; i--) {
dp[i] = dp[i + 1] + 1; // 状态1
for (int j = 0; j < W; j++) { // 枚举每个单词,查看是否匹配
pivot = i;
index = 0;
int len = dic[j].length;
while (pivot < L) { // 此时匹配失败
if (message[pivot] == dic[j][index]) // 若相等则迭代器均增加
{
pivot++;
index++;
} else
// 否则只增加message迭代器
pivot++;
if (index == len) { // 匹配成功
dp[i] = Min(dp[i], dp[pivot] + pivot - i - index); // 状态2
break;
}
}
}
}
}
System.out.println(dp[0]); // 输出结果
}
}