P1659 [国家集训队]拉拉队排练(回文自动机板子题)

做法一:套用回文自动机板子,求出每个本质不同的回文串出现的次数。
做法二:直接Manacher算法,可以想到的是,一个长度为5的回文串也一定包含一个长度为3的回文串,利用Manacher求出每个中点的最大回文串,从大到小维护长度大于当前i的奇回文串的前缀和。

const int N = 1e6 + 5;

struct PAM {
//主要可以解决以下问题
//1.本质不同回文串个数
//2.每个本质不同回文串出现的次数
//3.以某一个节点为结尾的回文串个数
#define KIND 26
	int n, last, tot;
	int len[N], trie[N][KIND], fail[N], S[N], num[N];
	ll cnt[N];
	//len[i]: 节点i所代表的回文串长度, fail[i]: 当前回文串的最长回文后缀(不包括自身)
	//cnt[i]: 节点i所代表的回文串的个数, S[i]: 第i次添加的字符, num[i]: 以第i个字符为结尾的回文串个数
	//last: 上一个字符构成最长回文串的位置,方便下一个字符的插入
	//tot: 总结点个数 = 本质不同的回文串的个数+2, n: 插入字符的个数 
	int newnode(int l) {
		f(i, 0, KIND - 1) trie[tot][i] = 0;
		cnt[tot] = 0, len[tot] = l, num[tot] = 0;
		return tot++;
	}
	inline void init() {
		tot = n = last = 0, newnode(0), newnode(-1);
		S[0] = -1, fail[0] = 1;
	}
	int get_fail(int x) { //获取fail指针
		while (S[n - len[x] - 1] != S[n]) x = fail[x];
		return x;
	}
	inline int insert(int c) { //插入字符
		c -= 'a';
		S[++n] = c;
		int cur = get_fail(last);
		//在节点cur前的字符与当前字符相同,即构成一个回文串
		if (!trie[cur][c]) { //这个回文串没有出现过
			int now = newnode(len[cur] + 2);
			fail[now] = trie[get_fail(fail[cur])][c];
			trie[cur][c] = now;
			num[now] = num[fail[now]] + 1; //更新以当前字符为结尾的回文串的个数
		}
		last = trie[cur][c];
		cnt[last]++; //更新当前回文串的个数
		return num[last]; //返回以当前字符结尾的回文串的个数
	}
	void count() { //统计每个本质不同回文串的个数
		ff(i, tot - 1, 0) cnt[fail[i]] += cnt[i];
	}
}pam;
char s[N];
int main()
{
	//freopen("in.txt", "r", stdin);
	ll n, k;
	scanf("%lld%lld", &n, &k);
	scanf("%s", s);
	pam.init();
	f(i, 0, n - 1)pam.insert(s[i]);
	pam.count();//统计每个本质不同的回文串的个数
	vector<pair<int, ll> > v;
	ll sum = 0;
	f(i, 0, pam.tot - 1)
	{
		if (pam.len[i] & 1)
		{
			v.push_back(make_pair( pam.len[i], pam.cnt[i]));//按第一元素:长度,排序
			sum += pam.cnt[i];
		}
	}
	if (sum < k) { puts("-1");return 0; }
	ll ans = 1;
	sort(v.begin(), v.end());
	int ed = v.size() - 1;
	ff(i,ed,0)
	{
		if (k >= v[i].second)
		{
			ans = ans * quickmod(v[i].first, v[i].second,mod)% mod;
			k -= v[i].second;
		}
		else
		{
			ans = ans * quickmod(v[i].first, k, mod) % mod;
			k = 0;
		}
		if (k == 0)break;
	}
	cout << ans << endl;
	return 0;
}
const int N = 1e6 + 5;

char Ma[N << 1], s[N];
int r[N << 1], cnt[N << 1];
void Manacher(char s[], int len)
{
	int l = 0;
	Ma[l++] = '$';
	Ma[l++] = '#';
	for (int i = 0;i < len;i++)
	{
		Ma[l++] = s[i];
		Ma[l++] = '#';
	}
	Ma[l] = 0;
	int mx = 0, id = 0;
	for (int i = 0;i < l;i++)
	{
		r[i] = mx > i ? min(r[2 * id - i], mx - i) : 1;
		while (Ma[i + r[i]] == Ma[i - r[i]])r[i]++;
		if (i + r[i] > mx)
		{
			mx = i + r[i];
			id = i;
		}
		++cnt[r[i] - 1];
	}
}
int main()
{
	//freopen("in.txt", "r", stdin);
	ll n, k;
	scanf("%lld%lld", &n, &k);
	scanf("%s", s);
	Manacher(s, n);
	ll ans = 1;
	ll now = 0;
	ff(i, 2 * n + 1, 0)
	{
		if (i%2==0) continue;
		now += cnt[i];
		if (now > k)
		{
			ans = ans * quickmod(i, k, mod) % mod;
			k = 0;
		}
		else
		{
			ans = ans * quickmod(i, now, mod) % mod;
			k -= now;
		}
		if (!k)break;
	}
	if (k)puts("-1");
	else cout << ans << endl;
	return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值