首先通过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的更多相关文章

  1. [BZOJ 4060] Word Equations

    Link: BZOJ 4060 传送门 Solution: 可以发现字符串间的关系可以构成一棵树 于是我们先用字符串哈希建树,再树形$dp$即可 设$dp[i][j]$为第$i$个节点从$P$字符串的 ...

  2. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  3. 要back的题目 先立一个flag

    要back的题目 目标是全绿!back一题删一题! acmm7 1003 1004 acmm8 1003 1004 sysu20181013 Stat Origin Title Solved A Gy ...

  4. 怎样将word文件转化为Latex文件:word-to-latex-2.56具体解释

    首先推荐大家读一读这篇博文:http://blog.csdn.net/ibingow/article/details/8613556 --------------------------------- ...

  5. DocX开源WORD操作组件的学习系列二

    DocX学习系列 DocX开源WORD操作组件的学习系列一 : http://www.cnblogs.com/zhaojiedi1992/p/zhaojiedi_sharp_001_docx1.htm ...

  6. 课程五(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 ...

  7. word公式编辑器公式

    Linear format equations and Math AutoCorrect in Word Applies To: Word 2016 Outlook 2016 Word 2013 Ou ...

  8. LaTeX:Figures, Tables, and Equations 插入图表和公式

    Figures To insert a figure in a LaTeX document, you write lines like this: \begin{figure} \centering ...

  9. word中添加Mathtype公式行间距改变问题

    转载链接:http://blog.sciencenet.cn/home.php?mod=space&uid=471807&do=blog&id=616838 最近碰到在word ...

随机推荐

  1. 他们在军训,我在搞 OI(一)

    Day 1 理论上,我现在不应该坐在电脑前打字,因为早在今天上午 6:20 全体新高一同学就坐车前往军(无)训(尽)基(炼)地(狱)了,而今天上午 6:20 我还在被窝里呢…… 没错,我旷掉了军训,然 ...

  2. hMailServer+foxmail配置局域网邮件服务器

    1.下载hMailServer并安装,请参考以下网址 https://www.hmailserver.org 2.安装foxmail,官网如下: http://www.foxmail.com/ 3.配 ...

  3. 转SISD、MIMD、SIMD、MISD计算机的体系结构的Flynn分类法

    1. 计算平台介绍 Flynn于1972年提出了计算平台的Flynn分类法,主要根据指令流和数据流来分类,共分为四种类型的计算平台,如下图所示: 单指令流单数据流机器(SISD) SISD机器是一种传 ...

  4. python-twisted

    环境:win7 64位,python 2.7.3 安装: http://twistedmatrix.com/Releases/Twisted/14.0/Twisted-14.0.0.win-amd64 ...

  5. Python获取目录、文件的注意事项

    Python获取指定路径下的子目录和文件有两种方法: os.listdir(dir)和os.walk(dir),前者列出dir目录下的所有直接子目录和文件的名称(均不包含完整路径),如 >> ...

  6. 【OpenStack】OpenStack系列17之OpenStack私有云设计二

  7. MyBatis3: There is no getter for property named 'code' in 'class java.lang.String'

    mybatis3  : mysql文如下,传入参数为string类型时‘preCode’,运行报错为:There is no getter for property named 'preCode' i ...

  8. 修改Tomcat服务器的默认端口号

    tomcat服务器的默认端口号是8080,我们也可以修改为其他端口号,并且在没有启动Apache,IIS等占用80端口的web服务时,我们也可以设置为80端口,这样在生产中域名之后就可以不带端口号了, ...

  9. Java for LeetCode 035 Search Insert Position

    Given a sorted array and a target value, return the index if the target is found. If not, return the ...

  10. JavaScript、Jquery选择题

    尚学堂Java EE软件工程师认证考试 试题库-选择题     一.    选择题(包括单选和双选) 1.B 在JavaScript中,以下变量命名非法的是(   )(选择一项) A. numb_1 ...