又见面了,还是原来的配方,还是熟悉的DP。。。。直接秒了。。。
The Cow Lexicon
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 7316 Accepted: 3421

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

Source

USACO 2007 February Silver

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

int n,m;
char wen[500];
int dp[500];

struct dict
{
    char word[100];
    char theLastLaw;
    int len;
}D[800];

int main()
{
    scanf("%d%d",&n,&m);
    getchar();
    for(int i=1;i<=m;i++)
    {
        scanf("%c",&wen);
    }
    for(int i=0;i<n;i++)
    {
        scanf("%s",D.word);
        D.len=strlen(D.word);
        D.theLastLaw=D.word[D.len-1];
    }
    memset(dp,0,sizeof(dp));
    for(int i=1;i<=m;i++)
    {
        dp=dp[i-1]+1;
        for(int j=0;j<n;j++)
        {
            if(wen==D[j].theLastLaw&&i>=D[j].len)
            {
           //     cout<<i<<": "<<wen<<" catch with: "<<D[j].word<<endl;
                int flag=D[j].len-1,delet=0,pos=-1;
                for(int k=i;k>0;k--)
                {
                    if(wen[k]==D[j].word[flag])
                        flag--;
                    else
                        delet++;
                    if(flag<0){ pos=k; break;}
                }
             //   cout<<"the flag: "<<flag<<"  catch end with: "<<pos<<"  , delet is: "<<delet<<endl;
                if(flag<0)
                {
                    if(pos==-1) pos=0;
                    dp=min(dp,dp[pos-1]+delet);
                }
            }
        }
    }
    printf("%d\n",dp);
    return 0;
}

* This source code was highlighted by YcdoiT. ( style: Codeblocks )

POJ 3267 The Cow Lexicon的更多相关文章

  1. poj 3267 The Cow Lexicon (动态规划)

    The Cow Lexicon Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8167   Accepted: 3845 D ...

  2. poj 3267 The Cow Lexicon(dp)

    题目:http://poj.org/problem?id=3267 题意:给定一个字符串,又给n个单词,求最少删除字符串里几个字母,能匹配到n个单词里 #include <iostream> ...

  3. POJ 3267 The Cow Lexicon 简单DP

    题目链接: http://poj.org/problem?id=3267 从后往前遍历,dp[i]表示第i个字符到最后一个字符删除的字符个数. 状态转移方程为: dp[i] = dp[i+1] + 1 ...

  4. POJ - 3267 The Cow Lexicon(动态规划)

    https://vjudge.net/problem/POJ-3267 题意 给一个长度为L的字符串,以及有W个单词的词典.问最少需要从主串中删除几个字母,使其可以由词典的单词组成. 分析 状态设置很 ...

  5. PKU 3267 The Cow Lexicon(动态规划)

    题目大意:给定一个字符串和一本字典,问至少需要删除多少个字符才能匹配到字典中的单词序列.PS:是单词序列,而不是一个单词 思路:                                     ...

  6. POJ 3267:The Cow Lexicon(DP)

    http://poj.org/problem?id=3267 The Cow Lexicon Time Limit: 2000MS   Memory Limit: 65536K Total Submi ...

  7. POJ 3267:The Cow Lexicon 字符串匹配dp

    The Cow Lexicon Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8905   Accepted: 4228 D ...

  8. POJ 3267-The Cow Lexicon(DP)

    The Cow Lexicon Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8252   Accepted: 3888 D ...

  9. POJ3267 The Cow Lexicon(DP+删词)

    The Cow Lexicon Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9041   Accepted: 4293 D ...

随机推荐

  1. Opencv step by step - 绘图

    有时候我们需要在图像上写一些字或者画一些简单的几何图案. 祭上代码: #include <cv.h> #include <highgui.h> int main(int arg ...

  2. ASP.NET MVC 5 入门教程 (1) 新建项目

    文章来源: Slark.NET-博客园 http://www.cnblogs.com/slark/p/mvc-5-get-started-create-project.html 下一节:ASP.NET ...

  3. bt协议详解 DHT篇(上)

    bt协议详解 DHT篇(上) 最近开发了一个免费教程的网站,突然产生了仔细了解bt协议的想法,这篇文章是bt协议详解系列的第三篇,后续还会写一些关于搜索和索引的东西,都是在开发这个网站的过程中学习到的 ...

  4. Github个人使用经历

    Github是什么? 简单来说Git是一个分布式的版本控制系统.GitHub可以托管各种git库,并提供一个web界面,但与其它像 SourceForge或Google Code这样的服务不同,Git ...

  5. 第一个C语言编译器是怎样编写的?

    首先向C语言之父Dennis MacAlistair Ritchie致敬! 当今几乎所有的实用的编译器/解释器(以下统称编译器)都是用C语言编写的,有一些语言比如Clojure,Jython等是基于J ...

  6. SQL Serve允许远程连接的解决方法

    (一)用户需要做的第一件事是检查SQL数据库服务器中是否允许远程链接.在SQL 2008服务器中可以通过打开SQL Server 2008管理项目(SQL Server 2008 Management ...

  7. 『开源』Slithice 2013 服务器集群 设计和源码

    相关介绍文章: <『设计』Slithice 分布式架构设计-支持一体式开发,分布式发布> <『集群』001 Slithice 服务器集群 概述> <『集群』002 Sli ...

  8. 分页pagination实现及其应用

    1.分页jquery.page.js //分页插件 /** 2014-08-05 ch **/ (function ($) { var ms = { init: function (obj, args ...

  9. xsd、wsdl生成C#类的命令行工具使用方法

    1.xsd生成C#类命令 示例:xsd <xsd文件路径> /c /o:<生成CS文件目录> <其他参数> 参数说明: /c 生成为cs文件,/d 生成DataSe ...

  10. 《TCP/IP详解卷1:协议》第17、18章 TCP:传输控制协议(1)-读书笔记

    章节回顾: <TCP/IP详解卷1:协议>第1章 概述-读书笔记 <TCP/IP详解卷1:协议>第2章 链路层-读书笔记 <TCP/IP详解卷1:协议>第3章 IP ...