POJ3267 DP

POJ3267 字符串匹配DP题

刚开始练DP题,有点不熟悉,也想了挺久了,题意是给一串字符串,每个奶牛有自己的字典,问在字符串里面最少删掉多少个字母能使字符串里面的单词都在奶牛的字典范围,如样例给的browndcodw,删掉中间的d和倒数第二个d,变成browncow,都是在奶牛的字典里面可以找到,所以输出2.
设dp[i]为到了第i个字符所需要删除的最少字符数,那么在dp[i]处就有两种方案,要么选择不匹配,直接删掉,那么dp[i] = dp[i-1]+1
要么选择匹配,想了一想如果从头开始匹配有点难弄,所以从尾开始匹配,弄两个指针pm,pd分别指向messige,和dic,如果相同就同时减一,不同就messige的指针减一,最后如果pd<0就说明匹配成功,那么此时dp[i]就可以更新为dp[i]=min(dp[i],dp[pm]+i-pm-len),其实不用那么麻烦,直接在不同的时候加一个计数器cnt,dp[i]=min(dp[i],dp[pm]+cnt)就可以了,不过第一种也不难理解,用笔算下就知道了,另外因为我输入的时候是从0开始就记录的,所以在匹配的时候有可能出现pd<0 pm<0的情况,要特别处理下,如果是从1开始记录的话就不用这么麻烦..(我会说因为这个我调试了一早上么…),具体的细节见代码吧..

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

代码

#include<iostream>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<string>
using namespace std;

int dp[605];
string messige;
string dic[605];

int main()
{
    int n,len;
    scanf("%d%d",&n,&len);
    cin >> messige;
    for (int i = 0;i<n;i++)
        cin >> dic[i];

    dp[0]=1;
    for (int i = 0;i<len;i++)
    {
        if (i)
        dp[i]=dp[i-1]+1;
        for (int j = 0;j<n;j++)
           if (dic[j][dic[j].size()-1] == messige[i] && dic[j].size()-1<=i)
           {
               int pd = dic[j].size()-1;
               int pm = i;
               int cnt = 0;
               while (pm >=0)
               {
                if (dic[j][pd]==messige[pm])
                {
                   pm--;pd--;
                }
                 else
                {
                   pm--;
                   cnt++;
                }

                if (pd<0)
                {
                   if (pm < 0)
                      dp[i] = min(dp[i],cnt);
                    else dp[i] = min(dp[i],dp[pm]+cnt);
                   break;
                }
               }
           }
    }

    printf("%d\n",dp[len-1]);
}

你可能感兴趣的:(POJ3267 DP)