跳到正文

题解

题解:P4600 [HEOI2012] 旅行问题

_Sorasaki_Hina_约 6 分钟
原文
文章信息与版本

文章信息

收录
2026年2月12日
更新
2026年2月13日
篇幅
约 6 分钟
题目
P4600

版本

  1. v2当前
  2. v1

比较友好的树剖与 AC 自动机。

题意

nn 个字符串,mm 次询问。 每次询问有四个数 x,y,z,wx,y,z,w,求: 第 xx 个串的前 yy 个字符构成的子串 S1S_1 与第 zz 个串的前 ww 个字符构成的子串 S2S_2 的最长公共后缀 ss,需要满足 ss 是这 nn 个字符串中某个串的前缀。

好绕,但很简洁。

思路

做题时的分析:

  1. 由于 S1,S2S_1,S_2 皆为前缀,所以我们考虑将这 nn 个字符串放到 AC 自动机里。
  2. 注意到 AC 自动机的 fail 数组的优秀性质:fail 指向所有模式串的前缀中匹配当前状态的最长后缀。 所以要求最长公共后缀,我们当然希望 S1S_1 和 S2S_2 跳 fail 指针跳的越少越好,这样 ss 会更长。 于是想到 LCA,所以要对 fail 树做树剖。

具体做法

  1. 对这 nn 个字符串建 AC 自动机,顺便把 Trie 树中每个节点(即每个串的前缀)的哈希值 hv 求出来。 顺便将每个前缀对应的 Trie 的节点记下来(pos 数组),方便处理询问。
  2. 求 fail 数组,建 fail 树。
  3. 树剖。
  4. 对于每个询问(x,y,z,wx,y,z,w),令 S1S_1 对应的 Trie 节点为 uu,S2S_2 对应的 Trie 节点为 vv。 求出 LCA⁡(u,v)\operatorname{LCA}(u,v) 的 hv 即可。

代码(有注释,马峰还行)

C++
#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#define re register
#define endl '\n'
#define ll long long
#define ull unsigned long long
#define db double
#define fi const int&
#define fl const ll&
#define ful const ull&
#define fc const char&
#define fs const string&
#define debug() puts("I Love Hina Forever")
using namespace std ;

const int N = 1e6 + 2,MOD = 1e9 + 7 ;
int n,m ;
string s[N] ;
vector<int> g[N],pos[N] ;

class ACAM {//AC自动机,个人喜欢封装
	private :
		#define geti(c) ((c) - 'a')
		int idx ;
		struct Trie {int fail,ch[26] ;ll hv ;}t[N] ;
	public :

		inline void insert (fs s,fi id) {//插入每个字符串
			re int p = 0 ;
			for (re fc c : s) {
				pos[id].push_back(p) ;//记录每个前缀对应的Trie中的编号(这里注意第0个位置存的0号点)
				re int j = geti(c) ;
				if (!t[p].ch[j]) t[p].ch[j] = ++idx ;
				t[t[p].ch[j]].hv = (t[p].hv * 26 + j) % MOD ;//算每个前缀的哈希值
				p = t[p].ch[j] ;
			}
			pos[id].push_back(p) ;//不要忘了最后一个字符,因为这里调了半小时
		}

		inline void build () {//求fail数组,建fail树
			queue<int> q ;
			for (re int i = 0 ; i < 26 ; ++i) if (t[0].ch[i]) q.push(t[0].ch[i]) ;
			while (!q.empty()) {
				re int u = q.front() ;q.pop() ;
				for (re int i = 0,v ; i < 26 ; ++i) {
					v = t[u].ch[i] ;
					if (v) t[v].fail = t[t[u].fail].ch[i],q.push(v) ;
					else t[u].ch[i] = t[t[u].fail].ch[i] ;
				}
			}

			for (re int i = 1 ; i <= idx ; ++i) g[t[i].fail].push_back(i) ;
		}

		inline ll gethv (fi u) {return t[u].hv ;}//得到前缀的哈希值
};
ACAM T ;

//树链剖分
int fa[N],son[N],top[N],siz[N],dep[N] ;

inline int LCA (int u,int v) {
	while (top[u] != top[v]) {
		if (dep[top[u]] < dep[top[v]]) swap(u,v) ;
		u = fa[top[u]] ;
	}
	if (dep[u] < dep[v]) swap(u,v) ;
	return v ;
}

inline void dfs1 (fi u,fi f) {
	siz[u] = 1,fa[u] = f,dep[u] = dep[f] + 1 ;
	for (re fi v : g[u]) {
		if (v != f) {
			dfs1(v,u) ;
			siz[u] += siz[v] ;
			if (!son[u] || siz[v] > siz[son[u]]) son[u] = v ;//这里因为根为0,所以(!son[u])这个条件不能省
		}
	}
}

inline void dfs2 (fi u,fi tp) {
	top[u] = tp ;
	if (son[u]) dfs2(son[u],tp) ;
	for (re fi v : g[u]) {
		if (v != fa[u] && v != son[u]) dfs2(v,v) ;
	}
}

int main () {
	ios_base::sync_with_stdio(0) ;
	cin.tie(0),cout.tie(0) ;

	cin >> n ;
	for (re int i = 1 ; i <= n ; ++i) cin >> s[i],T.insert(s[i],i) ;
	T.build() ;
	dfs1(0,0),dfs2(0,0) ;

	cin >> m ;
	while (m--) {
		re int x,y,z,w ;
		cin >> x >> y >> z >> w ;
		cout << T.gethv(LCA(pos[x][y],pos[z][w])) << endl ;
	}
	return 0 ;
}
原文