HNCPC 2022 K:Substrings Same as Prefix(SAM 纯板子)

CSG-CPC 1199:Substrings Same as Prefix 湖南省第十八届大学生计算机程序设计竞赛(HNCPC2022)

Description
We want to know how similar the content of a string is to its prefix. A string could gain a score of k when there is a non-prefix substring of length k that is the same as its prefix. Find the total score for a string.

Input
There are 20 test cases. Each case contains a string of English letters with length n.

1 ≤ n ≤ 105

Output
Each case one line, the score of the string.

Sample Input
abcabc
aaaa
Sample Output
6
10
Hint
For the first case, “a”, “ab”, “abc” are non-prefix substrings same as prefix and the score is 1+2+3=6.

For the second case, 3 “a”s, 2 “aa”s and 1 “aaa” are non-prefix substrings same as prefix. Then the score is 3+4+3=10.

Source
湖南省第十八届大学生计算机程序设计竞赛(HNCPC2022)
Author
CSGrandeur

赛前没有好好地复习 SAM,没出这题责任在我。

题意:

多组测试数据,每次输入一个长度最大为 1e5 的字符串 s。要求算出每次输入字符串的“得分”,“得分”的定义: 当有一个长度为 k 的 非前缀子串 与 s 的 前缀 相同时,s 可以获得 k 的分数,得到的总分为 s 的“得分”。

思路:

对每次输入的原串构建后缀自动机,并在 parent 树上进行 dp,求得每个节点代表子串的出现次数,之后遍历所有前缀,把前缀放 SAM 上跑,看是否有非前缀与之匹配,算出每次的得分,累加即可得到答案。

个人感觉是这道题的变形:P5231 [JSOI2012]玄武密码

时间复杂度: O ( n ) O(n) O(n)

代码:

#include<bits/stdc++.h>

using namespace std;

const int N = 1e5 + 10, M = N << 1;
int ch[M][52], fa[M], len[M], np = 1, tot = 1;
char s[N];
long long cnt[M];
vector<int> g[M];

inline void Clear()
{
	for (int i = 0; i <= tot; ++i) {
		memset(ch[i], 0, sizeof ch[i]);
		fa[i] = len[i] = cnt[i] = 0;
		g[i].clear();
	}
	np = tot = 1;
}

inline int get(char c) {	//给出的字符串可能有大写,这是个坑
	if (islower(c)) return c - 'A' - 6;
	else return c - 'A';
}

void extend(int c)
{
	int p = np; np = ++tot;
	len[np] = len[p] + 1, cnt[np] = 1;
	while (p && !ch[p][c]) {
		ch[p][c] = np;
		p = fa[p];
	}
	if (!p) {
		fa[np] = 1;
	}
	else {
		int q = ch[p][c];
		if (len[q] == len[p] + 1) {
			fa[np] = q;
		}
		else {
			int nq = ++tot;
			len[nq] = len[p] + 1;
			fa[nq] = fa[q], fa[q] = fa[np] = nq;
			while (p && ch[p][c] == q) {
				ch[p][c] = nq;
				p = fa[p];
			}
			memcpy(ch[nq], ch[q], sizeof ch[q]);
		}
	}
}

void dfs(int u)
{
	for (auto son : g[u]) {
		dfs(son);
		cnt[u] += cnt[son];
	}
}

inline void solve()
{
	for (int i = 0; s[i]; ++i) extend(get(s[i]));
	for (int i = 2; i <= tot; ++i) {
		g[fa[i]].emplace_back(i);
	}
	dfs(1);
	int p = 1;
	long long sum = 0;
	for (int i = 0; s[i]; ++i)
	{
		auto c = get(s[i]);
		p = ch[p][c];
		sum += (cnt[p] - 1) * (i + 1);
	}
	printf("%lld\n", sum);
}

signed main()
{
	while (~scanf("%s", s)) {
		solve();
		Clear();
	}

	return 0;
}

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值