zoukankan      html  css  js  c++  java
  • POJ3267——The Cow Lexicon(动态规划)

    The Cow Lexicon


    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

    题目大意:

        题意就是给出一个主串,和一本字典,问最少在主串删除多少字母,可以使其匹配到字典的单词序列.
        PS:是匹配单词序列,而不是一个单词

    解题思路:

        动态规划。网上多数是从后往前推。我从前往后推也AC了。发现更好理解一些。

        具体细节看代码。

    Code:

     1 /*************************************************************************
     2     > File Name: poj3267.cpp
     3     > Author: Enumz
     4     > Mail: 369372123@qq.com
     5     > Created Time: 2014年10月17日 星期五 18时28分18秒
     6  ************************************************************************/
     7 #include<iostream>
     8 #include<cstdio>
     9 #include<cstdlib>
    10 #include<string>
    11 #include<cstring>
    12 #include<list>
    13 #include<queue>
    14 #include<stack>
    15 #include<map>
    16 #include<set>
    17 #include<algorithm>
    18 #define MAXN 1001
    19 using namespace std;
    20 char dic[MAXN][MAXN];
    21 char mes[MAXN];
    22 int dp[MAXN];
    23 int N,M;
    24 int Incl(int i,int j) //判断字典j是否包含在字符串前i位内
    25 {
    26     int Ndic=strlen(dic[j])-1;
    27     if (dic[j][Ndic]!=mes[i]) return -1;
    28     while (1)
    29     {
    30         if (Ndic==-1||i==-1) break;
    31         if (dic[j][Ndic]==mes[i]) i--,Ndic--;
    32         else i--;
    33     }
    34     if (Ndic==-1) return i+1; //返回字典i匹配的第一个字符的位置。
    35     else return -1;
    36 }
    37 int solve()
    38 {
    39     for (int i=0; i<=M-1; i++)
    40     {
    41         if (i!=0)
    42             dp[i]=dp[i-1]+1;
    43         else
    44             dp[i]=1;
    45         for (int j=1; j<=N; j++)
    46         {
    47             int k=Incl(i,j); //判断字典j是否包含在字符串前i位内
    48             if (k!=-1)
    49             {
    50                 //回溯到字符串开头处时,需要特判。注意if语句!WA了好几遍
    51                 if (k-1<0&&dp[i]>i+1-k-strlen(dic[j]))
    52                     dp[i]=(i+1-k-strlen(dic[j]));
    53                 else if (dp[i]>dp[k-1]+i+1-k-strlen(dic[j]))
    54                     dp[i]=dp[k-1]+i+1-k-strlen(dic[j]);
    55             }
    56         }
    57     }
    58     return dp[M-1];
    59 }
    60 int main()
    61 {
    62     while (scanf("%d%d",&N,&M)!=EOF)
    63     {
    64         memset(dp,0,sizeof(dp));
    65         memset(dic,0,sizeof(dic));
    66         memset(mes,0,sizeof(mes));
    67         scanf("%s",mes);
    68         for (int i=1; i<=N; i++)
    69             scanf("%s",dic[i]);
    70         printf("%d
    ",solve());
    71     }
    72     return 0;
    73 }

        

  • 相关阅读:
    python json 和 pickle的补充 hashlib configparser logging
    go 流程语句 if goto for swich
    go array slice map make new操作
    go 基础
    块级元素 行内元素 空元素
    咽炎就医用药(慢性肥厚性咽炎)
    春季感冒是风寒还是风热(转的文章)
    秋季感冒 咳嗽 怎么选药
    解决IE浏览器“无法显示此网页”的问题
    常用的 css 样式 记录
  • 原文地址:https://www.cnblogs.com/Enumz/p/4095070.html
Copyright © 2011-2022 走看看