[luoguP2875] [USACO07FEB]牛的词汇The Cow Lexicon(DP)

传送门

f[i] 表示前 i 个字符去掉多少个 的最优解

直接暴力DP

——代码

 1 #include <cstdio>
 2 #include <cstring>
 3 #include <iostream>
 4
 5 int n, m, cnt, f[301];
 6 char s[301], a[601][26];
 7
 8 inline int read()
 9 {
10     int x = 0, f = 1;
11     char ch = getchar();
12     for(; !isdigit(ch); ch = getchar()) if(ch == ‘-‘) f = -1;
13     for(; isdigit(ch); ch = getchar()) x = (x << 1) + (x << 3) + ch - ‘0‘;
14     return x * f;
15 }
16
17 inline int min(int x, int y)
18 {
19     return x < y ? x : y;
20 }
21
22 int main()
23 {
24     int i, j, k, l;
25     n = read();
26     m = read();
27     scanf("%s", s + 1);
28     for(i = 1; i <= n; i++) scanf("%s", a[i] + 1);
29     for(i = 1; i <= m; i++)
30     {
31         f[i] = i;
32         for(j = 1; j <= n; j++)
33         {
34             cnt = 0;
35             l = strlen(a[j] + 1);
36             for(k = i; k; k--)
37             {
38                 if(a[j][l] == s[k]) l--;
39                 else cnt++;
40                 if(!l) break;
41             }
42             if(k) f[i] = min(f[i], f[k - 1] + cnt);
43         }
44     }
45     printf("%d\n", f[m]);
46     return 0;
47 }

时间: 2024-10-11 10:57:15

[luoguP2875] [USACO07FEB]牛的词汇The Cow Lexicon(DP)的相关文章

洛谷P2875 [USACO07FEB]牛的词汇The Cow Lexicon

P2875 [USACO07FEB]牛的词汇The Cow Lexicon 题目描述 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; sometime

bzoj1633 / P2875 [USACO07FEB]牛的词汇The Cow Lexicon

P2875 [USACO07FEB]牛的词汇The Cow Lexicon 三维dp 它慢,但它好写. 直接根据题意设三个状态: $f[i][j][k]$表示主串扫到第$i$个字母,匹配到第$j$个单词的第$k$位可以留下的最多字符数 当该位不选时,就传递上一位的数据$f[i][j][k]=f[i-1][j][k]$ 当该位可以匹配时: $if(a[i]==b[j][k]\&\&f[i-1][j][k-1])$$f[i][j][k]=max(f[i][j][k],f[i-1][j][k-1

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

poj3267--The Cow Lexicon(dp:字符串组合)

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

POJ 3267-The Cow Lexicon(DP)

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

The Cow Lexicon DP

Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10659   Accepted: 5116 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 sy

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"

POJ 3276 The Cow Lexicon DP-字符串匹配

点击打开链接 The Cow Lexicon Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 8325   Accepted: 3934 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'.

[USACO06JAN]牛的舞会The Cow Prom

[USACO06JAN]牛的舞会The Cow Prom 题目描述 The N (2 <= N <= 10,000) cows are so excited: it's prom night! They are dressed in their finest gowns, complete with corsages and new shoes. They know that tonight they will each try to perform the Round Dance. Only