BZOJ3075 : [Usaco2013]Necklace
首先对b串做kmp求出nxt数组。
设f[i][j]表示考虑了a的前i个字符,在b中匹配到了j的最长长度,按照kmp算法直接转移即可。
$ans=n-\max(f[n][j])$。
时间复杂度$O(nm)$。
#include<cstdio>
#include<cstring>
#define N 1010
char a[10010],b[N];int n,m,i,j,k,x,nxt[N],f[2][N],ans;
inline void up(int&a,int b){if(a<b)a=b;}
int main(){
scanf("%s%s",a+1,b+1),n=strlen(a+1),m=strlen(b+1);
for(i=2;i<=m;nxt[i++]=j){
while(j&&b[j+1]!=b[i])j=nxt[j];
if(b[j+1]==b[i])j++;
}
for(j=1;j<m;j++)f[0][j]=-1;
for(i=0;i<n;i++,x^=1){
for(j=0;j<m;j++)f[x^1][j]=-1;
for(j=0;j<m;j++)if(~f[x][j]){
up(f[x^1][j],f[x][j]);
for(k=j;k&&b[k+1]!=a[i+1];k=nxt[k]);
if(b[k+1]==a[i+1])k++;
up(f[x^1][k],f[x][j]+1);
}
}
for(j=0;j<m;j++)up(ans,f[x][j]);
return printf("%d",n-ans),0;
}
BZOJ3075 : [Usaco2013]Necklace的更多相关文章
- 2019.03.01 bzoj3075: [Usaco2013]Necklace(kmp+dp)
传送门 题意简述:给出S,TS,TS,T两个字串,∣S∣≤10000,∣T∣≤1000|S|\le10000,|T|\le1000∣S∣≤10000,∣T∣≤1000,问至少从SSS中删去几个字符能够 ...
- Usaco2012-2013 金组 题解 (暂缺Hill walk以及Figue eight)
https://files.cnblogs.com/files/Winniechen/usaco2012-2013.pdf 做的不是很好,还请见谅! 如果有什么疑问,可以QQ上找我. QQ号:1967 ...
- BZOJ-USACO被虐记
bzoj上的usaco题目还是很好的(我被虐的很惨. 有必要总结整理一下. 1592: [Usaco2008 Feb]Making the Grade 路面修整 一开始没有想到离散化.然后离散化之后就 ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- BZOJ3075,LG3082 [USACO13MAR]项链Necklace
题意 Bessie the cow has arranged a string of N rocks, each containing a single letter of the alphabet, ...
- BZOJ3075[USACO 2013 Mar Gold 3.Necklace]——AC自动机+DP
题目描述 给你一个长度为n的字符串A,再给你一个长度为m的字符串B,求至少在A中删去多少个字符才能使得B不是A的子串.注:该题只读入A和B,不读入长度,先读入A,再读入B.数据保证A和B中只含小写字母 ...
- HDU5730 Shell Necklace(DP + CDQ分治 + FFT)
题目 Source http://acm.hdu.edu.cn/showproblem.php?pid=5730 Description Perhaps the sea‘s definition of ...
- 2016 Multi-University Training Contest 1 H.Shell Necklace
Shell Necklace Time Limit: 16000/8000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)T ...
- hdu 5727 Necklace dfs+二分图匹配
Necklace/center> 题目连接: http://acm.hdu.edu.cn/showproblem.php?pid=5727 Description SJX has 2*N mag ...
随机推荐
- 经典sql语句
经典SQL语句大全 一.基础 1.说明:创建数据库CREATE DATABASE database-name 2.说明:删除数据库drop database dbname3.说明:备份sql serv ...
- ubuntu 快速安装jre
sudo add-apt-repository ppa:webupd8team/java sudo apt-get update sudo apt-get install oracle-java7-i ...
- 如何实现SSH断开后 进程仍然在后台运行
1.nohup命令功能:不挂断地运行命令,忽略HUP信号.语法:nohup command & 实例:nohup ping www.google.com & 转自: http://bl ...
- 【VirtualBox】VirtualBox的桥接网络模式,为啥网络不稳定?
网桥模式访问外网非常慢,经常卡死,ping时断时续 七搞八搞,反复重启了几次 TMD 就好了,也不知道什么情况,VirtualBox还是不太好使啊..... 网桥模式 设置 如下: 参考资料: ht ...
- Java for LeetCode 041 First Missing Positive
Given an unsorted integer array, find the first missing positive integer. For example, Given [1,2,0] ...
- BestCoder10 1002 Revenge of GCD(hdu 5019) 解题报告
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5019 题目意思:给出 X 和 Y,求出 第 K 个 X 和 Y 的最大公约数. 例如8 16,它们的公 ...
- 【mysql】执行mysql脚本
来源:http://zhidao.baidu.com/link?url=p78PlTJZlheB4uSKCkmZApg-3qrTIBCS3yI5LbGJLEAnUuO3-4GE6dLqq1LWC_kn ...
- 有时间测试dism
dism /capture-image /imagefile:d\win.win /capturedir:c:\ /name:win81 dism /export-image /winboot /so ...
- telnet 测试端口是否打开
[root@mysqld ~]# yum list |grep telnet telnet.x86_64 1:0.17-48.el6 @base telnet-server.x86_64 1:0.17 ...
- Reactor模式详解
转自:http://www.blogjava.net/DLevin/archive/2015/09/02/427045.html 前记 第一次听到Reactor模式是三年前的某个晚上,一个室友突然跑过 ...