Time Limit: 2000MS | Memory Limit: 65536K | |
Total Submissions: 9814 | Accepted: 4716 |
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 10 browndcodw cow milk white black brown farmer
Sample Output
2
Source
#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]);//结果
}
代码菜鸟,如有错误,请多包涵!!