• [poj 3267] The Cow Lexicon dp


    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

     题目意思时查找最多匹配单词的数量   从后向前匹配,  dp[i]表示[i, L-1]之间的最少删除个数

    不能匹配时 dp[i] = dp[i+1]+1  

    将字典的每一个单词与str[i]的子串开始向后匹配 若匹配成功 则dp[i] = dp[p]+p-i-m

    p为字典单词最后一位在str[i]子串位置的下一个 p-i-m即为多余的字符 如

    r o d w [p]

    r o    w

    有些细节需要注意 如字典字符串在原字符串中不完整存在的情况 字典字符串长度大于原字符串的情况

    #include <iostream>
    #include <stdio.h>
    #include <cstring>
    #include <algorithm>
    using namespace std;
    int w, l;
    char dic[610][30];
    char str[310];
    int dp[310];
    
    int main()
    {
        //freopen("1.txt", "r", stdin);
        scanf("%d%d", &w, &l);
        scanf("%s", str);
        for (int i = 0; i < w; i++)
            scanf("%s", dic[i]);
    
        dp[l] = 0;
        for (int i = l-1; i >= 0; i--) {
            dp[i] = dp[i+1]+1;
            for (int j = 0; j < w; j++) {
                int m = strlen(dic[j]);
                if (m <= l-i && dic[j][0] == str[i]) {
                    int p = i, q = 0;
                    while (q < m && p < l) {
                        if (str[p] == dic[j][q]) {
                            p++; q++;
                        }
                        else p++;
                    }
                    if (q == m) {
                        dp[i] = min(dp[i], dp[p]+p-i-m);
                    }
                }
            }
        }
        printf("%d
    ", dp[0]);
    
        return 0;
    }
  • 相关阅读:
    openstack首页默认跳转
    centos7.3挂在移动硬盘(亲测)
    管理openstack多region介绍与实践
    一款功能强悍的web磁盘管理工具 (A powerful web disk management tools)
    文件及目录的归属关系
    Linux 权限基础说明
    umask默认权限分配
    linux删除、读取文件原理
    linux 文件类型
    LINUX 目录结构
  • 原文地址:https://www.cnblogs.com/whileskies/p/7242142.html
Copyright © 2020-2023  润新知