跳到正文

题解

题解:AT_agc047_b [AGC047B] First Second

Objective约 11 分钟
原文
文章信息与版本

文章信息

收录
2025年12月12日
更新
2025年12月27日
篇幅
约 11 分钟

版本

  1. v3当前
  2. v2
  3. v1

点我前往博客园食用。

思路与算法

首先我们注意到,它的删除操作就是在前面任意长度中只保留 11 个字符,并保留后面剩下的整个串。

这个时候,由于刚刚之前学过 Trie,我第一时间想到了可以用后缀的 Trie 来维护后缀,来看哪些字符串有着相同的字符后缀。

我们来看看算法的具体实现。

根据样例,我们首先将字符串反向处理,可以建出这样的 Trie:

不会字符串反向建 Trie 的看过来
C++
void AddStrRev(std::string s) {
	int pos = 0;
	// 实际上就只要改这里(循环条件)
	for (int i = s.size() - 1; i >= 0; i--) {
		if (!nodes[pos].son[s[i] - 'a']) {
			nodes[pos].son[s[i] - 'a'] = ++num;
		}
		pos = nodes[pos].son[s[i] - 'a'];
	}
	nodes[pos].CntEnd++;
}

1

(记得在每个节点记录以该节点结尾的字符串数量)

(为什么图床的 logo 这么大啊)

然后,我们注意到,abcxyx 与 cyx 拥有相同的后缀(Trie 上的前缀),且 cyx 除去与 abcxyx 相同的后缀 yx 后只剩了 11 个字母 c。

注:我们这里令 ∣S∣|S| 表示字符串 SS 的长度。

我们令原字符串为 SS 变成的字符串为 TT,我们注意到当 ∣T∣>1|T| > 1 时,有 T[2:∣T∣]=S[∣S∣−∣T∣+2:∣S∣]T[2:|T|] = S[|S| - |T| + 2:|S|] 且 T[1]∈S[1,∣S∣−∣T∣+1]T[1] \in S[1, |S| - |T| + 1]。

这里,我们统计每个字符串能够变成的字符串个数。

首先,我们不管有多少个字符将要成为公共后缀,我们先统计所有的字符个数(因为有且仅有一个字符的字符串且该字符在当前查询的字符串中出现了也是可行的)。

2

(涂绿的部分表示这个节点标记了单词的结束,边上的 CntEndCntEnd 就是涂绿的节点的 CntEndCntEnd 值)

为什么使用 int 而不是用 bool

因为题目没说字符串会不一样。

首先我们在根节点进行搜索,我们发现它的子结点没有一个是有结束标记的,因此我们跳过,到下一层,并下一层字母所对应的 cnt[]cnt[] 减一(即 cnt[23]cnt[23])。

3

显然,在这次操作中同样没有子结点带有标记,我们继续向下。

4

这次我们惊奇的发现,它有一个子结点具有结束标记而且在我们的 cntcnt 数组中该节点的字符所对应的是有值的(即在我们当前判断的字符串当中剩余的用作前缀的部分),这时我们统计上它的子结点(son[2]son[2])的 CntEndCntEnd 值。 接下来的操作就比较同理了。

接着,我们继续向下统计。(懒得说了,自己看图) 5 6

这对吗???

7

停停停!我们并不会到达 B。反过来想,如果我们到达了 B,那么 A 的 CntEndCntEnd 就会被记入 ansans 中。因此,我们这里有一个简单的条件修改让它不到达 B 节点:i>0i > 0(修改循环条件)。可能不是很能理解,不过这一个操作我们反过来想,如果 i>0i > 0,那么意味着循环就只有 (∣S∣−1)(|S| - 1) 次,也就是只会搜 (∣S∣−1)(|S| - 1) 层。对于字符串 abcxyx,∣S∣=6|S| = 6,只会搜 55 层,由于 Trie 的根节点也搜过了,因此这个字符串的最后两个字符(节点)并不会被我们搜到。

文字可能讲不清,但是可以看代码,很好理解(自认为)。

本部分代码
C++
int query(std::string s) {
	// 计数
	int cnt[30];
	// 字符串 s 所能变成的其他字符串数
	int ans = 0;
	// 用于遍历 Trie
	int pos = 0;
	// 局部变量先清空
	memset(cnt, 0, sizeof(cnt));
	// 统计——在后续的统计中,我们需要知道哪些字符在 s 的前缀部分仍然可用
	for (int i = 0; i < s.size(); i++) {
		cnt[s[i] - 'a']++;
	}
	// 反着建的 Trie,所以也要反着遍历
	for (int i = s.size() - 1; i > 0; i--) {
		for (int k = 0; k < 26; k++) {
			// 如果有且仅有一个节点,且在公共后缀上的子结点有结束标记
			if (cnt[k] && nodes[pos].son[k]) {
				// 统计
				ans += nodes[nodes[pos].son[k]].CntEnd;
			}
		}
		// 如果没有节点了(理论上不会,但我 RE 了,就加了一句)
		if (!nodes[pos].son[s[i] - 'a']) {
			break;
		}
		pos = nodes[pos].son[s[i] - 'a'];
		// 成为了后缀的一部分,不可用
		cnt[s[i] - 'a']--;
	}
	return ans;
}

代码

C++
/*
 * @file first.cpp
 * @author Federico Prask
 * @date 2025-11-25
*/

#include <bits/stdc++.h>
#define file(s) \
	std::freopen(#s".in", "r", stdin), std::freopen(#s".out", "w", stdout)

using i64 = long long;
using ull = unsigned long long;
using i28 = __int128;
using f32 = double;
using ldb = long double;

inline int read() {
	register int x = 0, sign = 1;
	register char ch = getchar_unlocked();
	for (; !isdigit(ch); ch = getchar_unlocked()) {
		if (ch == '-') {
			sign = -1;
		}
		if (ch == EOF) {
			return EOF;
		}
	}
	for (; isdigit(ch); ch = getchar_unlocked()) {
		x = x * 10 + ch - '0';
	}
	return x * sign;
}

inline char readc() {
	register char ch = getchar_unlocked();
	for (; ch == ' ' || ch == '\n'; ch = getchar_unlocked()) {
		continue;
	}
	return ch;
}

inline std::string readstr() {
	register std::string s;
	register char ch = getchar_unlocked();
	for (; ch == ' ' || ch == '\n'; ch = getchar_unlocked()) {
		continue;
	}
	for (; isalnum(ch); ch = getchar_unlocked()) {
		s += ch;
	}
	return s;
}

constexpr int LEN = 1e6 + 5;

// 谁家好人天天做封装啊
namespace DataStruct {
	template<const int N>
	class Trie {
		private:
			int num = 0;
			struct Node{
				int CntEnd, son[30];
				char c;
			}nodes[N];
		public:
			void AddStrRev(std::string s) {
				int pos = 0;
				for (int i = s.size() - 1; i >= 0; i--) {
					if (!nodes[pos].son[s[i] - 'a']) {
						nodes[pos].son[s[i] - 'a'] = ++num;
					}
					pos = nodes[pos].son[s[i] - 'a'];
				}
				nodes[pos].CntEnd++;
			}
			int query(std::string s) {
				int cnt[30];
				int ans = 0;
				int pos = 0;
				memset(cnt, 0, sizeof(cnt));
				for (int i = 0; i < s.size(); i++) {
					cnt[s[i] - 'a']++;
				}
				for (int i = s.size() - 1; i > 0; i--) {
					for (int k = 0; k < 26; k++) {
						if (cnt[k] && nodes[pos].son[k]) {
							ans += nodes[nodes[pos].son[k]].CntEnd;
						}
					}
					if (!nodes[pos].son[s[i] - 'a']) {
						break;
					}
					pos = nodes[pos].son[s[i] - 'a'];
					cnt[s[i] - 'a']--;
				}
				return ans;
			}
	};
}

void work() {
	int n = read();
	std::string s[200005];
	DataStruct::Trie <LEN> t;
	for (int i = 1; i <= n; i++) {
		s[i] = readstr();
		t.AddStrRev(s[i]);
	}
	int ans = 0;
	for (int i = 1; i <= n; i++) {
		std::string tmp = s[i];
		ans += t.query(tmp);

	}
	printf("%d\n", ans);
	return ;
}

int main() {
	#ifndef ONLINE_JUDGE
	file(first);
	#endif

	#ifdef CPPIO
	std::cin.tie(0) -> sync_with_stdio(0);
	#endif

	work();

	return 0;
}
原文