2024.3.19 训练记录(20)

CF 432D Prefixes and Suffixes

题目链接

借着这道题复习了AC自动机,虽然这题并不需要,但还是有点价值的

kmp+dp

先用 kmp 求出 ne 数组

dp[i] 表示前缀 i 出现的次数,dp[ne[i]] += dp[i]

#include <bits/stdc++.h>

using namespace std;

#define int long long
using i64 = long long;

typedef pair<int, int> PII;
typedef pair<int, char> PIC;
typedef pair<double, double> PDD;
typedef pair<int, PII> PIII;

const int N = 1e5 + 10;
const int mod = 1e9 + 7;
const int maxn = 1e6 + 10;
const int mod1 = 954169327;
const int mod2 = 906097321;
const int INF = 0x3f3f3f3f3f3f3f3f;

void solve()
{
	string s;
	cin >> s;
	int n = s.size();
	vector<int> ne(n + 1);
	s = '.' + s;
	auto get_ne = [&](string s)
	{
		for (int i = 2; i <= n; i ++ )
		{
			int j = ne[i - 1];
			while (j && s[i] != s[j + 1]) j = ne[j];
			if (s[i] == s[j + 1]) j ++ ;
			ne[i] = j;
		}
	};
	get_ne(s);
	vector<int> dp(n + 1, 1);
	for (int i = n; i > 0; i -- )
		dp[ne[i]] += dp[i];
	vector<PII> ans;
	while (n)
	{
		ans.push_back({n, dp[n]});
		n = ne[n];
	}
	sort(ans.begin(), ans.end());
	cout << ans.size() << '\n';
	for (auto t : ans) cout << t.first << ' ' << t.second << '\n';
}

signed main()
{
	ios::sync_with_stdio(false);
	cin.tie(0), cout.tie(0);

	int t = 1;
	// cin >> t;
	while (t--)
	{
		solve();
	}
}
  • 10
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Texcavator

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值