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. Map.Entry遍历Map

    Map.entry是Java中的一个接口.每一个Map.entry代表一个map对象. 可以通过 Map是java中的接口,Map.Entry是Map的一个内部接口,它表示map中的一个实体(及一个k ...

  2. Codeforces 821E Okabe and El Psy Kongroo(矩阵快速幂)

    E. Okabe and El Psy Kongroo time limit per test 2 seconds memory limit per test 256 megabytes input ...

  3. Ural 1297 Palindrome(Manacher或者后缀数组+RMQ-ST)

    1297. Palindrome Time limit: 1.0 second Memory limit: 64 MB The “U.S. Robots” HQ has just received a ...

  4. 【题解】IOI2005River 河流

    一节语文课想出来的玩意儿,调了几个小时……可见细心&好的代码习惯是有多么的重要 (:へ:) 不过,大概竞赛最令人开心的就是能够一点一点的感受到自己的进步吧,一天比一天能够自己想出更多的题,A题 ...

  5. [Leetcode] remove nth node from the end of list 删除链表倒数第n各节点

    Given a linked list, remove the n th node from the end of list and return its head. For example, Giv ...

  6. 做一个所见即所得的CSS效果

    style 也是标签(在非ie内核的浏览器中支持),我们将style设置成 contenteditable的时候,那么那的内容就可以编辑了.仔细的体验下,如果我们将背景修改成红色的.那么只要书写完,立 ...

  7. HDU2255:奔小康赚大钱(KM算法)

    奔小康赚大钱 Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Subm ...

  8. HDU 4417 划分树写法

    Problem Description Mario is world-famous plumber. His “burly” figure and amazing jumping ability re ...

  9. js和jquery修改背景颜色的区别

    html: <HTML> <head> <meta http-equiv="content-type" content="text/html ...

  10. ListView使用--文章集锦

    详解ListView加载网络图片的优化,让你轻松掌握! ListView具有多种item布局--实现微信对话列 关注公众号,分享干货,讨论技术