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]+1);$

注意不允许有重叠单词所以要判断前一位

当匹配完一个单词时,就要跳到一个新单词的第一位上,那么:

$f[i][j][1]=max(f[i][j][1],f[i-1][u][len[u]]+1)$

最后用主串的长度减去可留下字符数就是答案了。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 using namespace std;
 5 int max(int &a,int &b){return a>b?a:b;}
 6 int m,n,f[305][605][30],len[605],ans;
 7 char a[305],b[605][30];
 8 void output(int x){//debug
 9     cout<<"x:"<<x<<endl;
10     for(int i=1;i<=m;++i,cout<<endl)
11         for(int j=1;j<=len[i];++j)
12             cout<<f[x][i][j]<<" ";
13     cout<<endl<<endl;
14 }
15 int main(){
16     scanf("%d%d",&m,&n); scanf("%s",a+1);
17     for(int i=1;i<=m;++i)
18         scanf("%s",b[i]+1),len[i]=strlen(b[i]+1);
19     for(int i=1;i<=n;++i)
20         for(int j=1;j<=m;++j){
21             for(int k=1;k<=len[j];++k){
22                 f[i][j][k]=f[i-1][j][k];
23                 if(a[i]==b[j][k]&&f[i-1][j][k-1])
24                     f[i][j][k]=max(f[i][j][k],f[i-1][j][k-1]+1);
25             }
26             if(a[i]==b[j][1])
27                 for(int u=1;u<=m;++u)
28                     f[i][j][1]=max(f[i][j][1],f[i-1][u][len[u]]+1);
29         }
30     for(int i=1;i<=m;++i) ans=max(ans,f[n][i][len[i]]);
31     printf("%d",strlen(a+1)-ans);
32     cout<<endl;
33     return 0;
34 }

原文地址:https://www.cnblogs.com/kafuuchino/p/10063465.html

时间: 2024-10-13 01:33:58

bzoj1633 / P2875 [USACO07FEB]牛的词汇The Cow Lexicon的相关文章

洛谷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

[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(;

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"

[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

P2863 [USACO06JAN]牛的舞会The Cow Prom

洛谷——P2863 [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 D

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

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'.

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

[POJ 3267] The Cow Lexicon 训练计划里把这题排到了topo里....然后我就这么死盯研究了一周topo算法(期间经历了三个人生风波....大物考试 高数考试跟模电考试----)啥不说了--上dp代码----没错 这是个dp!...赤果果的dp..就呢么傻呆呆地研究Topo算法--结果没研究出来.. 题意是给一个字符串和m个单词组成的字典,问最少删除几个字母能让这个字符串变成由字典中几个单词首位链接组成的字符串 dp思路还算好想 逆推 dp数组的下标是遍历字符串的起点 然