跳到正文

题解

题解:P12202 [COI 2022] 回文子串 / Madioničar

ddxrS约 1 分钟
原文
文章信息与版本

文章信息

发布
2025年4月21日
收录
2026年10月18日
更新
2026年11月21日
篇幅
约 1 分钟
获赞
5
题目
P12202

版本

  1. v1当前

可以依次枚举每一个回文中心,并记录下当前的答案。我们没必要求出每一个回文中心的最长回文子串,我们只需要求出它能够比当前答案大多少即可。因此,直接从当前答案的长度开始询问,若是回文子串就把答案加一,否则看下一个回文中心。因为回文子串的长度可能为奇数或偶数,所以需要分别求出这两种不同答案,总共只需要 2n2n 次。

C++
#include<bits/stdc++.h>
using namespace std;
int n;
bool ask(int l, int r) {
	if(l <= 0 || r > n) return false;
	printf("? %d %d", l, r); cout<<endl;
	int p; scanf("%d", &p);
	return p;
}
int main() {
	scanf("%d", &n);
	int ans1 = 1, ans2 = 0;
	for(int i = 1; i <= n; i++) {
		while(ask(i - ans1, i + ans1)) ans1++;
		while(ask(i - ans2, i + ans2 + 1)) ans2++;
	}
	printf("! %d", max(ans1 * 2 - 1, ans2 * 2));
    cout<<endl;
    cerr<<clock() * 1.0 / CLOCKS_PER_SEC<<'\n';
	return 0;
}
原文