ICPC Pacific Northwest Regional Contest 2017 A Odd Palindrome

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
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;
}

你可能感兴趣的:(manacher)