又见面了,还是原来的配方,还是熟悉的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. error C3872: "0xa0": 此字符不允许在标识符中使用

    整理:这是因为直接复制代码的问题.0xa0是十六进制数,换成十进制就是160,表示汉字的开始. 解决办法:在报错的代码行检查两边的空格,用英文输入法的空格替换掉. 万恶的网络,万恶的word,这些无厘 ...

  2. try-catch和throw,throws的区别和联系

    转载:http://blog.sina.com.cn/s/blog_62148d1e0100hkqc.html 区别一:throw 是语句抛出一个异常:throws 是方法抛出一个异常: throw语 ...

  3. 第七章 new的三步曲

    这章是本系列文章的重点,这章揭示了js对象的真正本质 看下面的事例 var a = new b(); 等价于 ①var a={}; ②a.__proto__=b.prototype; ③b.call( ...

  4. Unity3D UGUI中ScrollRect的一些知识点

    需求 这几天在公司里,项目需要将游戏游戏中的2D城堡界面在拉动的时候显示出3D的拉近效果.当时是在Cocos2d-x下实现的.回家的时候自己重新用Unity实现的了一遍. 虽然现在Unity已经到了5 ...

  5. Unity3D 游戏计时功能实现

    最近工作实在是太忙了,没办法认真写博客,但是还是要好好记录下日常的学习. 需求 各类游戏中都大量运用到计时功能,不管是直接显示的在前端UI,还是后台运行. 思路 Unity中提供了Time类可以方便的 ...

  6. hello,world!

    这可以算是我的第一篇博客了吧?真正意义上的开始... 我于2016年6月毕业于山东英才学院信工学院计算机管理系,至今已有近半年.几经周转,总算是准备稳定下来了.于是,当初的博客想法提上了日程. 本人工 ...

  7. 获取Web.config配置节

    static string GetAppSetting(string key) { var appSetting = ConfigurationManager.AppSettings[key]; if ...

  8. Coding the Matrix (0):映射、复数和域

    1. 非常好的 Python 教程 <深入 Python 3.0> 以及 IBM 开发社区的博客探索 Python. 2. 子集: s 是 S 的子集 >>>S = {2 ...

  9. excel导入数据库

    日常工作中,感觉一些基础知识需要做下笔记,可能是刚毕业的缘故吧,还保持着做笔记的习惯,但根据以往经验,纸质笔记最多保持一年,过后想找已是难过登天.电子版笔记感觉很不错,尤其是发布到网络中.笔记内容是本 ...

  10. 在Myeclipse buildpath 加server lib (server runtime)/项目导入时报错:The import javax.servlet.http.HttpServletRequest cannot be resolved

    来源于:http://blog.csdn.net/dingqinghu/article/details/8805922 http://yl-fighting.iteye.com/blog/140946 ...