AC日记——[HEOI2012]旅行问题 bzoj 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的更多相关文章
- AC日记——[HNOI2008]GT考试 bzoj 1009
1009 思路: KMP上走DP(矩阵加速): DP[i][j]表示当前在第i位,同是匹配到不吉利串的第j位的方案数: 代码: #include <bits/stdc++.h> using ...
- AC日记——明明的烦恼 bzoj 1005
1005 思路: prufer编码+组合数: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 1005 #de ...
- AC日记——Mato的文件管理 bzoj 3289
3289 思路: 莫队求区间逆序对个数,树状数组维护: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 500 ...
- AC日记——[Scoi2010]序列操作 bzoj 1858
1858 思路: 恶心: 代码: #include <bits/stdc++.h> using namespace std; #define maxn 100005 struct Tree ...
- AC日记——[ZJOI2007]报表统计 bzoj 1058
1058 思路: 平衡树的题: 然而我的平衡树写一次炸一次QwQ: 而且各种tle: 所以stl水过: 代码: #include <set> #include <cstdio> ...
- AC日记——[JSOI2007]建筑抢修 bzoj 1029
1029 思路: 贪心,而且,stl水过: 然而神特么输出que.size()就错! 代码: #include <queue> #include <cstdio> #inclu ...
- AC日记——[JSOI2008]火星人prefix bzoj 1014
1014 思路: 平衡树+二分答案+hash: 好了懂了吧. 代码: #include <cstdio> #include <cstring> #include <ios ...
- AC日记——[HAOI2007]覆盖问题 bzoj 1052
1052 思路: 二分答案: 二分可能的长度: 然后递归判断长度是否可行: 先求出刚好覆盖所有点的矩形: 可行的第一个正方形在矩形的一个角上: 枚举四个角上的正方形,然后删去点: 删去一个正方形后,递 ...
- AC日记——[SCOI2008] 着色方案 bzoj 1079
1079 思路: dp: 我们如果dp方程为15维,每维记录颜色还有多少种: 不仅tle,mle,它还re: 所以,我们压缩一下dp方程: 方程有6维,第i维记录有多少种颜色还剩下i次: 最后还要记录 ...
随机推荐
- C# 获取方法所在的 命名空间 类名 方法名
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
- 【bzoj3514】Codechef MARCH14 GERALD07加强版 LCT+可持久化线段树
题目描述 N个点M条边的无向图,询问保留图中编号在[l,r]的边的时候图中的联通块个数. 输入 第一行四个整数N.M.K.type,代表点数.边数.询问数以及询问是否加密.接下来M行,代表图中的每条边 ...
- android中常见的命名及其特点详解
Paseal命名法 Paseal命名法特点:String MyName-DelphiInt MyAge每个单词首字母大写 Camel命名法 Camel(驼峰的意思)命名法特点:String myNam ...
- 'com.alibaba.fastjson.support.spring.FastJsonpResponseBodyAdvice' is。。。。
com.alibaba.fastjson版本1.2.43版本在通过xml方式配置spring的时候会出现这个个奇怪的问题: Class 'com.alibaba.fastjson.support.sp ...
- 【BZOJ 3232】圈地游戏 二分+SPFA判环/最小割经典模型
最小割经典模型指的是“一堆元素进行选取,对于某个元素的取舍有代价或价值,对于某些对元素,选取后会有额外代价或价值”的经典最小割模型,建立倒三角进行最小割.这个二分是显然的,一开始我也是想到了最小割的那 ...
- linux+GraphicsMagick 安装
转摘自:http://blog.csdn.net/fhqsse220/article/details/12995763 GraphicsMagick 安装 下载软件:download:ftp://ft ...
- JavaScript学习笔记——浅拷贝、深拷贝
参考自:http://www.cnblogs.com/yichengbo/archive/2014/07/10/3835882.html 一.数组的深浅拷贝 在使用JavaScript对数组进行操作的 ...
- 【Foreign】最大割 [线性基]
最大割 Time Limit: 15 Sec Memory Limit: 256 MB Description Input Output Sample Input 3 6 1 2 1 1 2 1 3 ...
- HDU5772 String problem
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others) Total Submission ...
- 【洛谷 P1337】[JSOI2004]平衡点 / 吊打XXX (模拟退火)
题目链接 正解就算了吧,谁叫我理生化 语数外 政史地都菜呢 模拟退火真玄学,不知道发生了什么就跑出答案了,原理就算了吧,能用(pianfen)就好. 当重物平衡时,势能一定是最小的,于是当我随机出一个 ...