BZOJ4060 : [Cerc2012]Word equations
首先通过hash建树
设f[i][j]表示第i个特殊符号从P的第j位开始匹配能到达哪里
记忆化搜索,对于底层贪心匹配。
#include<cstdio>
#include<cstring>
using std::strlen;
const int N=510,M=2010;
char a[10],tmp[10],b[N][10],s[M];
int T,n,m,i,j,k,h[N],son[N][2],len[N],f[N][M];
inline int hash(char a[]){
int t=0,j=strlen(a);
for(int i=0;i<5;i++)t=t*27+(i<j?(a[i]-'A'+1):0);
return t;
}
int F(int x,int y){
if(~f[x][y])return f[x][y];
if(son[x][0])return f[x][y]=F(son[x][1],F(son[x][0],y));
int j=y;
for(int i=0;j<m&&i<len[x];i++)if(b[x][i]==s[j])j++;
return f[x][y]=j;
}
int main(){
for(scanf("%d",&T);T--;puts(F(j,0)<m?"NO":"YES")){
scanf("%d",&n);
for(i=1;i<=n;i++){
scanf("%s%s%s",a,tmp,b[i]),h[i]=hash(a);
if(b[i][0]>='a'&&b[i][0]<='z')son[i][0]=0,len[i]=strlen(b[i]);
else son[i][0]=hash(b[i]),scanf("%s",tmp),scanf("%s",tmp),son[i][1]=hash(tmp);
}
for(i=1;i<=n;i++)if(son[i][0])for(j=0;j<2;j++)for(k=1;k<=n;k++)if(son[i][j]==h[k]){son[i][j]=k;break;}
scanf("%s%s",a,s),m=strlen(s);
for(i=1;i<=n;i++)for(j=0;j<=m;j++)f[i][j]=-1;
for(i=hash(a),k=1;k<=n;k++)if(i==h[k]){j=k;break;}
}
return 0;
}
BZOJ4060 : [Cerc2012]Word equations的更多相关文章
- [BZOJ 4060] Word Equations
Link: BZOJ 4060 传送门 Solution: 可以发现字符串间的关系可以构成一棵树 于是我们先用字符串哈希建树,再树形$dp$即可 设$dp[i][j]$为第$i$个节点从$P$字符串的 ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- 要back的题目 先立一个flag
要back的题目 目标是全绿!back一题删一题! acmm7 1003 1004 acmm8 1003 1004 sysu20181013 Stat Origin Title Solved A Gy ...
- 怎样将word文件转化为Latex文件:word-to-latex-2.56具体解释
首先推荐大家读一读这篇博文:http://blog.csdn.net/ibingow/article/details/8613556 --------------------------------- ...
- DocX开源WORD操作组件的学习系列二
DocX学习系列 DocX开源WORD操作组件的学习系列一 : http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_sharp_001_docx1.htm ...
- 课程五(Sequence Models),第二 周(Natural Language Processing & Word Embeddings) —— 1.Programming assignments:Operations on word vectors - Debiasing
Operations on word vectors Welcome to your first assignment of this week! Because word embeddings ar ...
- word公式编辑器公式
Linear format equations and Math AutoCorrect in Word Applies To: Word 2016 Outlook 2016 Word 2013 Ou ...
- LaTeX:Figures, Tables, and Equations 插入图表和公式
Figures To insert a figure in a LaTeX document, you write lines like this: \begin{figure} \centering ...
- word中添加Mathtype公式行间距改变问题
转载链接:http://blog.sciencenet.cn/home.php?mod=space&uid=471807&do=blog&id=616838 最近碰到在word ...
随机推荐
- 高流量站点NGINX与PHP-fpm配置优化
导读 使用Nginx搭配PHP已有7年的经历,这份经历让我们学会如何为高流量站点优化NGINX和PHP-fpm配置. 以下正是这方面的一些提示和建议: 1. 将TCP切换为UNIX域套接字 1. 将T ...
- change column to bigint
今天存储数据的时候报错,发现是3435065640超出了常规int的存储长度, RangeError (3435065640 is out of range for ActiveRecord::Typ ...
- 使用twisted.web实现代理服务器
简单的实现谷歌的代理: 架构就是下面这么简单. ================= my server outside GFW | <----------------------> ...
- Controller之间传递数据:Block传值
http://itjoy.org/?p=420 前边我们介绍过属性传值和协议传值,这里介绍一下块传值,块类似于C中的函数指针.在Controller中传递数据非常方便,还是继续上一章的例子,将数据从S ...
- 74 使用BitSet输出数组中的重复元素
[本文链接] http://www.cnblogs.com/hellogiser/p/using-bitset-to-print-duplicate-elements-of-array.html [题 ...
- 安装完mysql后用root不能su成mysql
现象: debian的机器用aptitude install mysql-server-5.1 后,用id mysql 可看出已经建了mysql用户,但是用root来su mysql 不成功,/var ...
- php字符串处理总结
php字符串处理是php基础中重要的一部分,总结并整理了一下 1.最简单的,字符串输出单引号和双引号的区别(定义字符串用单引号和双引号都可以) $str='hello'; echo "str ...
- Delphi经验总结(3)
------------------------------------------------------- ◇删掉程序自己的exe文件 procedure TForm1.FormClose(Sen ...
- mybatisforeach循环,传入多个参数
上代码: controller: @RequestMapping(value = "/findPage", method = RequestMethod.POST) @Respon ...
- 【Qt】学习笔记(一)
1.setupUi(this) : setupUi(this)是由.ui文件生成的类的构造函数这个函数的作用是对界面进行初始化它按照我们在Qt设计器里设计的样子把窗体画出来 setupUi(this) ...