解题:USACO07FEB The Cow Lexicon
第一次做Trie上dp,感谢 @i207M 的资瓷
对子串们建立一棵Trie,设$dp[i][j]$表示到母串第$i$位为止在$Trie$上的$j$号节点时的最小修改数量,然后就可以枚举母串各位与Trie的节点dp了。
首先有两个显然的转移
$dp[i+1][j]=min(dp[i+1][j],dp[i][j]+1)$(将当前字符认为是噪音,继续考虑下一位)
$dp[i+1][son[s[i+1]]]=min(dp[i+1][son[s[i+1]]],dp[i][j])$(在Trie上匹配一个)
然后发现并不对......
事实上还有一个转移,当我们匹配完一个子串时,我们又回到了Trie的根节点,即对于每个结束节点$j$有
$dp[i][0]=min(dp[i][0],dp[i][j])$
好像凉了,转移回根节点就出环了。不过事实上只需要单独把根节点拿出来最后转移即可,就是在每一位转移结束后讨论一下根节点的情况
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=,M=,L=;
int trie[M*L][],las[M],dp[N][M*L];
char talk[N],word[M][L];
int n,m,tot,ans=1e9;
bool ed[M*L];
void Insert(char *s,int id)
{
int len=strlen(s),nde=;
for(int i=;i<len;i++)
{
int val=s[i]-'a'+;
if(!trie[nde][val])
trie[nde][val]=++tot;
nde=trie[nde][val];
}
las[id]=nde,ed[nde]=true;
}
int main ()
{
scanf("%d%d%s",&n,&m,talk+);
for(int i=;i<=m;i++)
talk[i]=talk[i]-'a'+;
for(int i=;i<=n;i++)
scanf("%s",word[i]),Insert(word[i],i);
memset(dp,0x3f,sizeof dp),dp[][]=;
for(int i=;i<=m;i++)
{
for(int j=;j<=tot;j++)
{
dp[i+][j]=min(dp[i+][j],dp[i][j]+);
if(ed[j]) dp[i][]=min(dp[i][],dp[i][j]);
int nde=trie[j][(int)talk[i+]];
if(nde) dp[i+][nde]=min(dp[i+][nde],dp[i][j]);
}
dp[i+][]=min(dp[i+][],dp[i][]+);
int nde=trie[][(int)talk[i+]];
if(nde) dp[i+][nde]=min(dp[i+][nde],dp[i][]);
}
printf("%d",dp[m][]);
return ;
}
解题:USACO07FEB The Cow Lexicon的更多相关文章
- bzoj1633 / P2875 [USACO07FEB]牛的词汇The Cow Lexicon
P2875 [USACO07FEB]牛的词汇The Cow Lexicon 三维dp 它慢,但它好写. 直接根据题意设三个状态: $f[i][j][k]$表示主串扫到第$i$个字母,匹配到第$j$个单 ...
- 洛谷P2875 [USACO07FEB]牛的词汇The Cow Lexicon
P2875 [USACO07FEB]牛的词汇The Cow Lexicon 题目描述 Few know that the cows have their own dictionary with W ( ...
- POJ3267——The Cow Lexicon(动态规划)
The Cow Lexicon DescriptionFew know that the cows have their own dictionary with W (1 ≤ W ≤ 600) wor ...
- POJ3267 The Cow Lexicon(DP+删词)
The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 9041 Accepted: 4293 D ...
- POJ 3267 The Cow Lexicon
又见面了,还是原来的配方,还是熟悉的DP....直接秒了... The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submis ...
- POJ 3267:The Cow Lexicon(DP)
http://poj.org/problem?id=3267 The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submi ...
- The Cow Lexicon
The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8815 Accepted: 4162 Descr ...
- HDOJ-三部曲-1015-The Cow Lexicon
The Cow Lexicon Time Limit : 4000/2000ms (Java/Other) Memory Limit : 131072/65536K (Java/Other) To ...
- poj3267--The Cow Lexicon(dp:字符串组合)
The Cow Lexicon Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 8211 Accepted: 3864 D ...
随机推荐
- 04-matplotlib-柱形图
import numpy as np import matplotlib.pyplot as plt # 柱形图 # 例一 N =5 y = [15,28,10,30,25] index = np.a ...
- Metasploit拿Shell
进入metasploit系统 msfconsole Nmap端口扫描 nmap –sV IP(或者域名),如果机器设置有防火墙禁ping,可以使用nmap -P0(或者-Pn) –sV IP(或者域名 ...
- 读书笔记 之java编程思想
本阶段我正在读java的编程思想这本书,这本书只是刚读了第一章的一部分,有些有些要记得所以记录下来, 我认为要记得有就是复用这样可以对对象进行增强,将一个类作为下一个类中基本类型,这样达到的服用的目的 ...
- ASP.net四则运算《《《策略模式
Calculator.cs using System; using System.Collections.Generic; using System.Linq; using System.Web; / ...
- SDPA: Toward a Stateful Data Plane in Software-Defined Networking
文章名称:SDPA: Toward a Stateful Data Plane in Software-Defined Networking 发表时间:2017 期刊来源:IEEE/ACM Trans ...
- PHP面试题一
http://www.viphper.com/?p=28 1.用PHP打印出前一天的时间格式是2006-5-10 22:21:21(2分) $a = date("Y-m-d H:i:s&qu ...
- 关于supervisor无法监控golang代码的解决方法
之前一直都是使用如下方式运行go代码 # go run test.go 这种运行方式是直接编译运行go代码,虽然在调试的时候没出什么问题,但是在使用supervisor监控的时候,会提示如下错误:(b ...
- 搭建企业级Docker Registry -- Harbor
Harbor 是一个企业级的 Docker Registry,可以实现 images 的私有存储和日志统计权限控制等功能,并支持创建多项目(Harbor 提出的概念),基于官方 Registry V2 ...
- svmtrain和svmpredict简介
转自:http://blog.sina.com.cn/s/blog_4d7c97a00101bwz1.html 本文主要介绍了SVM工具箱中svmtrain和svmpredict两个主要函数: (1) ...
- 《简明Python教程》学习笔记
<简明Python教程>是网上比较好的一个Python入门级教程,尽管版本比较老旧,但是其中的基本讲解还是很有实力的. Ch2–安装Python:下载安装完成后,在系统的环境变量里,在Pa ...