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;
}

  

时间: 2024-08-29 17:23:11

BZOJ4060 : [Cerc2012]Word equations的相关文章

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

Figures To insert a figure in a LaTeX document, you write lines like this: \begin{figure} \centering \includegraphics[width=3.0in]{imagefile1} \caption{Caption for figure} \label{fig:sample_figure} \end{figure} The whole block is enclosed between \be

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

转载链接:http://blog.sciencenet.cn/home.php?mod=space&uid=471807&do=blog&id=616838 最近碰到在word文档中用mathtype插入公式后导致正文间距过大的问题,苦恼了很久,差点就要改用latex了,但最终找到了解决办法. 在网上找到的办法有: 1.在word中点击“文件”菜单下的“页面设置”项.在“文档网格”标签页中的“网格”一栏,勾选“无网格”项(如图).这样能很大程度上缓解行距不等的情况,然后再进行公式大

调整Word文档中所有公式的大小!

步骤一:先运行MathType,点击“Size”菜单中的“Define”命令,中文版的“尺寸”——“定义”.  点击“Size”——“Define” 步骤二:打开“Define Sizes”对话框,我们可以在“Full”后的输入框中要调整的字号大小(比如word字体为小五,则相应的公式大小调整为9 pt).公式中其它的元素会自动进行相应的调整的,所以一般情况下可以不做其它改动.点击“OK”按钮,关闭对话框.在打开的“定义尺寸”对话框,可以在“完全”后面输入要调整的字号大小,单击“确定”.  De

Word中 简单宏的使用

 (注意:打开文档时按住 Shift 键可以阻止 AutoOpen 宏运行) 1:Word中能够自动运行的默认宏代码名称及触发条件如下 -------------------------------------------------------- 1.名称:AutoExec 条件:启动Word或加载全局模板 2.名称:AutoNew 条件:每次生成新文档时 3.名称:AutoOpen 条件:每次打开一个已有文档时 4.名称:AutoClose 条件:每次关闭文档时 5.名称:AutoExit

LeetCode58 Length of Last Word

题目: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined as a character sequence consists of non-space

Qt 向word中插入文字(使用QAxWidget和QAxObject)

pro 文件中要加入 CONFIG += qaxcontainer 2. main.cpp [cpp] view plain copy #include <QApplication> #include <QAxWidget> #include <QAxObject> int main(int argc, char* argv[]) { QApplication app(argc, argv); QAxWidget *word=new QAxWidget("Wo

Atitit.office&#160;word&#160;&#160;excel&#160;&#160;ppt&#160;pdf&#160;的web在线预览方案与html转换方案&#160;attilax&#160;总结

Atitit.office word  excel  ppt pdf 的web在线预览方案与html转换方案 attilax 总结 1. office word  excel pdf 的web预览要求1 1.1. 显示效果要好1 1.2. 可以自定义显示界面1 1.3. 不需要控件,兼容性好1 1.4. 支持编辑操作1 2. 纯html预览解决之道(自由的格式)1 3. 转换swf flash方案2 4. 转换pdf方式..更多的浏览器已经直接支持pdf格式查看2 5. 控件方式2 6. Hyb

Java [Leetcode 58]Length of Last Word

题目描述: Given a string s consists of upper/lower-case alphabets and empty space characters ' ', return the length of last word in the string. If the last word does not exist, return 0. Note: A word is defined as a character sequence consists of non-spa

C#生成Word报表(插入文字、表格和图片)

回家前一个晚上,老板临时安排让写一个读取txt文件的数据和多个图片数据生成一个word文档.时间给得太短只能是回家继续加班,百度真是个好东西这里引用一下我参考的博客http://blog.csdn.net/jiutao_tang/article/details/6574740/ http://xuzhihong1987.blog.163.com/blog/static/2673158720109188465634/  这个博客讲的用录制word宏的方法做参考很有用.感谢这位博友 静下心,认真做,