ICPC Pacific Northwest Regional Contest 2017 A Odd Palindrome

该博客探讨如何确定一个字符串是否所有回文子串的长度都是奇数,即'奇数回文串'。通过给出的样例输入和输出,说明了判断标准,并提出了使用Manacher算法来解决这个问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://www.elijahqi.win/archives/4041
We say that a string is oddodd if and only if all palindromic substrings of the string have odd length.

Given a string ss, determine if it is oddodd or not.

A substring of a string ss is a nonempty sequence of consecutive characters from ss. A palindromic substring is a substring that reads the same forwards and backwards.

1 Input

The input consists of a single line containing the string ss (1 ≤ |s| ≤ 100)(1≤∣s∣≤100).

It is guaranteed that ss consists of lowercase ASCIIASCII letters (‘a’–‘z’)(‘a’–‘z’) only.

2 Output

If ss is oddodd, then print “Odd.Odd.” on a single line (without quotation marks). Otherwise, print “Or\ not.Or not.” on a single line (without quotation marks).

样例输入复制
1
2
3
4
amanaplanacanalpanama
madamimadam
annamyfriend
nopalindromes
样例输出复制
1
2
3
4
Odd.
Odd.
Or not.
Odd.
用manacher找出所有回文串的长度,判断下是否是奇数即可

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22

#include<bits/stdc++.h>
using namespace std;
char s[110],a[2200];
int p[220];
int main(){
	freopen("a.in","r",stdin);
	while(~scanf("%s",s)){
		int n=strlen(s);
		for (int i=1;i<=n;++i) a[(i<<1)]=s[i-1],a[(i<<1)-1]='#';
		n<<=1;++n;a[n]='#';
		int mx=0,id=0,ans=0;bool flag=1;
		for (int i=1;i<=n;++i){
			if (i+p[i]<mx) p[i]=min(mx-i,p[(id<<1)-i]);else p[i]=1;
			while( a[i+p[i]]==a[i-p[i]]&&i-p[i]>0&&i+p[i]<=n) ++p[i];
			if (i+p[i]>mx) mx=i+p[i],id=i;
			if (p[i]==1) continue;
			if (!((p[i]-1)%2)) flag=0;
		}
		if (flag) puts("Odd.");else puts("Or not.");
	}
	return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值