poj3267(The Cow Lexicon)

题目地址:The Cow Lexicon

题目大意:

   奶牛有自己的识别单词的语言,它有自己的字典序列,如果给一串字符不符合奶牛的字典里的单词,奶牛就无法识别,你的任务就是找出给的字符串中包含给出奶牛字典的单词,至少从主串里删除几个字符,使主串只包含奶牛字典里的单词,不包含多于的字符。

解题思路:

    动态规划dp。 DP一直都不太懂,不看解题报告根本推不出来状态方程,入门水平都不够,需要好好练习学习该知识点。

状态方程: dp[i] i 代表从i到L最少需要删除的字符个数。

首先分为两种思想:

一是不能匹配成功  : dp[i]=dp[i+1]+1;(从后往前推)

二是匹配成功:        dp[i]=min(dp[i],dp[p1+1]+p1+1-i-len);

解释一下第二个式子:

dp[p1+1]+p1+1-i-len.    因为能够匹配成功,从字符串数组 i 开始匹配,p1代表和字典字符串匹配结束位于主串的位置。 p1+1-i 代表主串用了多少个字符与字典字符串匹配成功。然后-len 代表需要删除的字符个数,然后 + dp[p1+1] 意为加上从p1+1位置到L的最小删除的个数。  所以整个式子代表dp[i]匹配成功时最少删除的个数。  因为从主串 i 位置到p1位置可能匹配很多次奶牛字典字符串,这时我们应该选取较小的dp[i]。

代码:

 1 #include <algorithm>
 2 #include <iostream>
 3 #include <sstream>
 4 #include <cstdlib>
 5 #include <cstring>
 6 #include <cstdio>
 7 #include <string>
 8 #include <bitset>
 9 #include <vector>
10 #include <queue>
11 #include <stack>
12 #include <cmath>
13 #include <list>
14 #include <map>
15 #include <set>
16 using namespace std;
17 /***************************************/
18 #define ll long long
19 #define int64 __int64
20 #define PI 3.1415927
21 /***************************************/
22 const int INF = 0x7f7f7f7f;
23 const double eps = 1e-8;
24 const double PIE=acos(-1.0);
25 const int d1x[]= {0,-1,0,1};
26 const int d1y[]= {-1,0,1,0};
27 const int d2x[]= {0,-1,0,1};
28 const int d2y[]= {1,0,-1,0};
29 const int fx[]= {-1,-1,-1,0,0,1,1,1};
30 const int fy[]= {-1,0,1,-1,1,-1,0,1};
31 /*vector <int>map[N];map[a].push_back(b);int len=map[v].size();*/
32 /***************************************/
33 void openfile()
34 {
35     freopen("data.in","rb",stdin);
36     freopen("data.out","wb",stdout);
37 }
38 priority_queue<int> qi1;
39 priority_queue<int, vector<int>, greater<int> >qi2;
40 /**********************华丽丽的分割线,以上为模板部分*****************/
41 int min(int a,int b)
42 {
43     if (a<b)
44         return a;
45     return b;
46 }
47 int main()
48 {
49     int n,m;
50     while(scanf("%d%d",&m,&n)!=EOF)
51     {
52         char s1[305],s2[605][305];
53         int dp[305];
54         memset(s1,0,sizeof(s1));
55         memset(s2,0,sizeof(s2));
56         memset(dp,0,sizeof(dp));
57         int i,j;
58         scanf("%s",s1);
59         for(i=0; i<m; i++)
60             scanf("%s",s2[i]);
61         int len1=strlen(s1);
62         dp[len1]=0;
63         for(i=len1-1; i>=0; i--)
64         {
65             dp[i]=dp[i+1]+1;
66             for(j=0; j<m; j++)
67             {
68                 int len2=strlen(s2[j]);
69                 int p1=i;
70                 int p2=0;
71                 while(len1-i>=len2&&s1[i]==s2[j][0])
72                 {
73                     if (s1[p1++]==s2[j][p2])
74                     {
75                         p2++;
76                     }
77                     if (p2==len2)
78                     {
79                         dp[i]=min(dp[i],dp[p1]+p1-i-len2); //这里没有p1+1,原因:p1++;
80                         break;
81                     }
82                     if(p1>=len1)
83                         break;
84
85                 }
86             }
87         }
88         printf("%d\n",dp[0]);
89     }
90     return 0;
91 }

poj3267(The Cow Lexicon)

时间: 2025-01-01 05:45:43

poj3267(The Cow Lexicon)的相关文章

POJ3267——The Cow Lexicon(动态规划)

The Cow Lexicon DescriptionFew 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 word

[USACO12FEB]牛券Cow Coupons(堆,贪心)

[USACO12FEB]牛券Cow Coupons(堆,贪心) 题目描述 Farmer John needs new cows! There are N cows for sale (1 <= N <= 50,000), and FJ has to spend no more than his budget of M units of money (1 <= M <= 10^14). Cow i costs P_i money (1 <= P_i <= 10^9), b

POJ3267 The Cow Lexicon(DP+删词)

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

BZOJ 1633: [Usaco2007 Feb]The Cow Lexicon 牛的词典

题目 1633: [Usaco2007 Feb]The Cow Lexicon 牛的词典 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 401  Solved: 216[Submit][Status] Description 没有几个人知道,奶牛有她们自己的字典,里面的有W (1 ≤ W ≤ 600)个词,每个词的长度不超过25,且由小写字母组成.她们在交流时,由于各种原因,用词总是不那么准确.比如,贝茜听到有人对她说"browndcodw"

android源码大放送(实战开发必备),免费安卓demo源码,例子大全文件详细列表

免费安卓demo源码,例子大全文件详细列表 本列表源码永久免费下载地址:http://www.jiandaima.com/blog/android-demo 卷 yunpan 的文件夹 PATH 列表 卷序列号为 0000-73EC E:. │ jiandaima.com文件列表生成.bat │ 例子大全说明.txt │ 本例子永久更新地址~.url │ 目录列表2016.03.10更新.txt │ ├─前台界面 │ ├─3D标签云卡片热门 │ │ Android TagCloudView云标签

POJ 3169 Layout (图论-差分约束)

Layout Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6574   Accepted: 3177 Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 <= N <= 1,000) cows numbered 1..N standing along a

POJ1990--POJ 1990 MooFest(树状数组)

Time Limit: 1000MSMemory Limit: 30000K Total Submissions: 8141Accepted: 3674 Description Every year, Farmer John's N (1 <= N <= 20,000) cows attend "MooFest",a social gathering of cows from around the world. MooFest involves a variety of e

Android应用程序UI硬件加速渲染的预加载资源地图集服务(Asset Atlas Service)分析

我们知道,Android系统在启动的时候,会对一些系统资源进行预加载.这样不仅使得应用程序在需要时可以快速地访问这些资源,还使得这些资源能够在不同应用程序之间进行共享.在硬件加速渲染环境中,这些预加载资源还有进一步优化的空间.Android系统提供了一个地图集服务,负责将预加载资源合成为一个纹理上传到GPU去,并且能够在所有的应用程序之间进行共享.本文就详细分析这个预加载资源地图集服务的实现原理. 老罗的新浪微博:http://weibo.com/shengyangluo,欢迎关注! 资源预加载

1675: [Usaco2005 Feb]Rigging the Bovine Election 竞选划区(题解第一弹)

1675: [Usaco2005 Feb]Rigging the Bovine Election 竞选划区 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 196  Solved: 116[Submit][Status][Discuss] Description It's election time. The farm is partitioned into a 5x5 grid of cow locations, each of which hold