The Cow Lexicon
Time Limit: 2000MS Memory Limit: 65536K
Total Submissions: 11846 Accepted: 5693
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

就是问你删多少个字母让他成为字典里的单词构成的

这是我的垃圾的错误超时的代码,等有空再用优先队列优化一下,卡的就是三重循环,我觉得我就是被制裁了。
好好学习DP,听学长说比赛的时候DP不是我们这个水平的人做的,我也知道DP很难,各种优化,甚至现在基础的都不会,要加油。

include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#include <fstream>
#include<set>
using namespace std;
string a,b[1000];
queue<string> dd;
set<string>ww;
string del(string a,string b);
int ans;
int main()
{int m,n;std::ios::sync_with_stdio(false);while(!dd.empty()) dd.pop();ww.clear();cin>>m>>ans;cin>>a;for(int i=0;i<m;i++){cin>>b[i];if(a==b[i]){cout<<0<<endl;return 0;}string tem=del(a,b[i]);if(a!=tem)dd.push(tem);}while(!dd.empty()){string demo=dd.front();ans=min(ans,(int)demo.size());if(!ans){cout<<0<<endl;return 0;}for(int i=0;i<m;i++){string tem=del(demo,b[i]);if(demo!=tem){string tee=del(tem,b[i]);while(tee!=tem){tem=tee;tee=del(tem,b[i]);}if(tem.size()<=ans)if(ww.insert(tem).second)dd.push(tem);}}dd.pop();}cout<<ans<<endl;return 0;
}
string del(string a,string b)
{int j=0;string tem;string w=a;bool flag=0;for(int i=0;i<a.size();i++){if(flag) tem.push_back(a[i]);else if(a[i]==b[j]) j++;else tem.push_back(a[i]);if(j==b.size())flag=1;}if(flag) return tem;else return w;
}

ACcode

#include <iostream>
#include <cstdio>
#include <cstring>
using namespace std;
const int maxl = 305;
string sentence;    //要解码的句子
string  words[601]; //字典中的句子
int w, l, d[maxl];      //d[i]表示地i个
int main()
{scanf("%d%d", &w, &l);cin>> sentence;for(int i = 0; i < w; i++)  cin>>words[i];d[l] = 0;for(int i = l-1; i >= 0; i--) {d[i] = d[i+1] + 1;for(int j = 0; j < w; j++) {int len = words[j].size();if(sentence[i] == words[j][0] && l-i >= len) {int pSentence = i, pWords = 0;while(pSentence < l) {if(words[j][pWords] == sentence[pSentence]) {pSentence++;  pWords++;}else    pSentence++;if(pWords == len) {d[i] = min(d[i], d[pSentence]+(pSentence-i)-len);}}}}}//for(int i = 0; i < 10; i++)  printf("%d ", d[i]);  printf("\n");printf("%d\n", d[0]);return 0;
}

POJ 3267为什么优先队列超时,DP就能过,难过相关推荐

  1. POJ 2955 Brackets (区间DP)

    题目链接:http://poj.org/problem?id=2955 Brackets Time Limit: 1000MS   Memory Limit: 65536K Total Submiss ...

  2. poj 3071 Football(概率dp)

    http://poj.org/problem? id=3071 大致题意:有2^n个足球队分成n组打比赛.给出一个矩阵a[][],a[i][j]表示i队赢得j队的概率.n次比赛的流程像这样France ...

  3. poj 2817 WordStack (状态dp)

    http://poj.org/problem?id=2817 这个题的意思是第一行给出case数N (1 <= N <= 10),然后给出N个单词,每个一行,当输入不是正整数的时候结束.每 ...

  4. POJ 3280 Cheapest Palindrome(DP 回文变形)

    题目链接:http://poj.org/problem?id=3280 题目大意:给定一个字符串,可以删除增加,每个操作都有代价,求出将字符串转换成回文串的最小代价 Sample Input 3 4 ...

  5. poj 2051 Argus(优先队列)

    题目链接: http://poj.org/problem?id=2051 思路分析: 优先级问题,使用优先队列求解:当执行某个任务后,再增加一个任务到队列中, 该任务的优先级为执行任务的时间加上其时间 ...

  6. poj 1862 Stripies/优先队列

    原题链接:http://poj.org/problem?id=1862 简单题,贪心+优先队列主要练习一下stl大根堆 写了几种实现方式写成类的形式还是要慢一些... 手打的heap: 1: 1 #i ...

  7. POJ 1947 Rebuilding Roads (树dp + 背包思想)

    题目链接:http://poj.org/problem?id=1947 一共有n个节点,要求减去最少的边,行号剩下p个节点.问你去掉的最少边数. dp[u][j]表示u为子树根,且得到j个节点最少减去 ...

  8. poj 3590 The shuffle Problem——DP+置换

    题目:http://poj.org/problem?id=3590 bzoj 1025 的弱化版.大概一样的 dp . 输出方案的时候小的环靠前.不用担心 dp 时用 > 还是 >= 来转 ...

  9. POJ 3133 Manhattan Wiring(插头DP)

    题目链接:http://poj.org/problem?id=3133 题意:n*m的格子中有两个2和两个3,其余是空白或障碍.找出两条路径分别连接2和3,不经过障碍且不相交.使得两条路径长度和最短? ...

最新文章

  1. idea创建xml文件 却没有xml模板
  2. sublime text2如何连接FTP/SFTP——图文详细教程
  3. CNN-4: GoogLeNet 卷积神经网络模型
  4. WdatePicker日期控件使用与值获取,以及选择日期完毕触发事件
  5. English Grammar
  6. 开源GIS(十三)——openlayers通过geoserver中WFS添加要素
  7. UVALive 3135--Argus+自己定义优先队列的优先规则
  8. [Javascript]把html内容复制到剪贴板
  9. CoolFire系列讲座 第7讲
  10. 关于GraphEasy的简单捣腾
  11. matlab sort函数
  12. 【操作系统-Windows】组件服务 DCOM 配置编辑安全属性
  13. php融云开发文档,融云应用公众服务开发者文档 - 融云 RongCloud
  14. react+amcharts5柱状图demo
  15. oracle错误代码大全(超详细)
  16. linux可执行文件剪裁
  17. 下一个排列,下一字典序
  18. MATLAB算法实战应用案例精讲-【回归算法】偏最小二乘回归(PLS)(附MATLAB、R语言和Python代码)
  19. Windows8程序开发小技巧---在代码中使用Segoe UI Symbol图标字体及常用图标列表
  20. CERTI 4.0.0 成功编译安装(Windows10+VS2022)

热门文章

  1. C语言编程猜谜语,简单的谜语大全及答案
  2. 野人与传教士过河java_传教士和野人过河(经典MC问题)
  3. java正则 找出数字_Java使用正则表达式实现找出数字功能示例
  4. python 控件叠加_Python3 tkinter基础 Checkbutton anchor for生成多个控件并西对齐
  5. ASP.NET CORE 之 在IIS上部署MVC项目
  6. 查看win10版本方法,及win10升级方法
  7. Oracle:集群因子
  8. 日志采集框架Flume、Flume介绍、概述、运行机制、Flume采集系统结构图(1、简单结构、复杂结构)...
  9. 《文明之光 第二册》一一10.1 罗卡尔角的夕阳—— 葡、西的殖民时代(1)
  10. JavaScript 的内部字符编码是 UCS-2 还是 UTF-16