2746

思路:

  建立ac自动机,然后把fail树抽出来;

  然后在fail树上走lca(神奇);

代码:

#include <cstdio>
#include <vector>
#include <cstring>
#include <iostream>
#include <algorithm> using namespace std; #define maxn 1000005
#define mod 1000000007LL struct TreeNodeType {
int id; long long dis; TreeNodeType *next[],*fail; TreeNodeType()
{
id=,dis=,fail=NULL;
for(int i=;i<;i++) next[i]=NULL;
}
};
struct TreeNodeType *map[maxn<<],*que[maxn<<],*root; int n,m,deep[maxn<<],f[maxn<<],top[maxn<<],head[maxn<<];
int E[maxn<<],V[maxn<<],tot,cnt,len,size[maxn<<]; char ch[maxn<<]; vector<int>vec[maxn<<]; inline void in(int &now)
{
char Cget=getchar();now=;
while(Cget>''||Cget<'') Cget=getchar();
while(Cget>=''&&Cget<='')
{
now=now*+Cget-'';
Cget=getchar();
}
} void dfs1(int now,int fa)
{
f[now]=fa,deep[now]=deep[fa]+,size[now]=;
for(int i=head[now];i;i=E[i])
{
if(V[i]==fa) continue;
dfs1(V[i],now),size[now]+=size[V[i]];
}
} void dfs2(int now,int chain)
{
top[now]=chain;int pos=;
for(int i=head[now];i;i=E[i])
{
if(V[i]==f[now]) continue;
if(size[V[i]]>size[pos]) pos=V[i];
}
if(pos) dfs2(pos,chain);else return ;
for(int i=head[now];i;i=E[i])
{
if(V[i]==pos||V[i]==f[now]) continue;
dfs2(V[i],V[i]);
}
} inline int lca(int x,int y)
{
for(;top[x]!=top[y];)
{
if(deep[top[x]]<deep[top[y]]) swap(x,y);
x=f[top[x]];
}
if(deep[x]>deep[y]) swap(x,y);
return x;
} int main()
{
in(n);root=new TreeNodeType;
root->id=++tot,map[root->id]=root;
for(int i=;i<=n;i++)
{
scanf("%s",ch),len=strlen(ch);
TreeNodeType *now=root;int pos;
for(int j=;j<len;j++)
{
pos=ch[j]-'a';
if(now->next[pos]==NULL)
{
now->next[pos]=new TreeNodeType,now->next[pos]->id=++tot;
map[tot]=now->next[pos],now->next[pos]->dis=(now->dis*26LL+pos)%mod;
}
vec[i].push_back(now->next[pos]->id),now=now->next[pos];
}
}
int h=,tail=,u,v;que[]=root;
while(h<tail)
{
TreeNodeType *now=que[h++],*temp=NULL;
for(int i=;i<;i++)
{
if(now->next[i]==NULL) continue;
if(now==root) now->next[i]->fail=root;
else
{
temp=now->fail;
while(temp!=NULL)
{
if(temp->next[i]!=NULL)
{
now->next[i]->fail=temp->next[i];
break;
}
temp=temp->fail;
}
if(temp==NULL) now->next[i]->fail=root;
}
que[tail++]=now->next[i];
u=now->next[i]->id,v=now->next[i]->fail->id;
E[++cnt]=head[u],V[cnt]=v,head[u]=cnt;
E[++cnt]=head[v],V[cnt]=u,head[v]=cnt;
}
}
dfs1(,),dfs2(,);
in(m);int a,a1,b,b1;
for(;m--;)
{
in(a),in(a1),in(b),in(b1);
printf("%lld\n",map[lca(vec[a][a1-],vec[b][b1-])]->dis);
}
return ;
}

AC日记——[HEOI2012]旅行问题 bzoj 2746的更多相关文章

  1. AC日记——[HNOI2008]GT考试 bzoj 1009

    1009 思路: KMP上走DP(矩阵加速): DP[i][j]表示当前在第i位,同是匹配到不吉利串的第j位的方案数: 代码: #include <bits/stdc++.h> using ...

  2. AC日记——明明的烦恼 bzoj 1005

    1005 思路: prufer编码+组合数: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 1005 #de ...

  3. AC日记——Mato的文件管理 bzoj 3289

    3289 思路: 莫队求区间逆序对个数,树状数组维护: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 500 ...

  4. AC日记——[Scoi2010]序列操作 bzoj 1858

    1858 思路: 恶心: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 100005 struct Tree ...

  5. AC日记——[ZJOI2007]报表统计 bzoj 1058

    1058 思路: 平衡树的题: 然而我的平衡树写一次炸一次QwQ: 而且各种tle: 所以stl水过: 代码: #include <set> #include <cstdio> ...

  6. AC日记——[JSOI2007]建筑抢修 bzoj 1029

    1029 思路: 贪心,而且,stl水过: 然而神特么输出que.size()就错! 代码: #include <queue> #include <cstdio> #inclu ...

  7. AC日记——[JSOI2008]火星人prefix bzoj 1014

    1014 思路: 平衡树+二分答案+hash: 好了懂了吧. 代码: #include <cstdio> #include <cstring> #include <ios ...

  8. AC日记——[HAOI2007]覆盖问题 bzoj 1052

    1052 思路: 二分答案: 二分可能的长度: 然后递归判断长度是否可行: 先求出刚好覆盖所有点的矩形: 可行的第一个正方形在矩形的一个角上: 枚举四个角上的正方形,然后删去点: 删去一个正方形后,递 ...

  9. AC日记——[SCOI2008] 着色方案 bzoj 1079

    1079 思路: dp: 我们如果dp方程为15维,每维记录颜色还有多少种: 不仅tle,mle,它还re: 所以,我们压缩一下dp方程: 方程有6维,第i维记录有多少种颜色还剩下i次: 最后还要记录 ...

随机推荐

  1. Activiti工作流(一)——Activiti Diagram

    工作流解决在多个参与者之间按照某种预定义的规则传递文档.信息或任务的过程自动进行,从而实现某个预期的业务目标,或者促使此目标的实现. 使用Eclipse开发,需要安排工作流插件,详情见下面. Name ...

  2. StrutsResultSupport的使用

    在有特殊情况时:如果没有异常信息,但是有错误并且有错误信息等内容:此时也需要进行友好的错误处理的话,那么可以借助StrutsResultSupport 返回结果类型来实现特定处理.此种方式先需要继承S ...

  3. 对Web作用域变量进行迭代

    <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> <%@ pa ...

  4. [Leetcode] Merge k sorted lists 合并k个已排序的链表

    Merge k sorted linked lists and return it as one sorted list. Analyze and describe its complexity. 思 ...

  5. 洛谷 P2414 [NOI2011]阿狸的打字机 解题报告

    P2414 [NOI2011]阿狸的打字机 题目背景 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机. 题目描述 打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母 ...

  6. JS Cookie相关操作

    function setCookie(cookieName, cookieValue, expires) { // 设置Cookie function getCookieName(cookieName ...

  7. EditPlus直接连接Linux服务器编辑文本文件

    填写好:描述,ip地址,用户名,密码, 然后点下面的高级选项: 然后返回上一个页面,继续 确定 OK: 然后,在主界面左侧点倒三角: 就可以选择我们之前配置的远程服务器地址,弹出提示框 点确定, 就连 ...

  8. Fabric证书解析

    一.证书目录解析   通过cryptogen生成所有证书文件后,以peerOrgannizations的第一个组织树org1为例,每个目录和对应文件的功能如下:   ca: 存放组织的根证书和对应的私 ...

  9. JSOI2008 星球大战 [并查集]

    题目描述 很久以前,在一个遥远的星系,一个黑暗的帝国靠着它的超级武器统治者整个星系. 某一天,凭着一个偶然的机遇,一支反抗军摧毁了帝国的超级武器,并攻下了星系中几乎所有的星球.这些星球通过特殊的以太隧 ...

  10. [hdu 2298] 物理推导+二分答案

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2298 #include<bits/stdc++.h> using namespace st ...