您的位置:首页 > 其它

CFgym:Palindromization(字符串Hash)

2017-05-12 01:36 393 查看
K. Palindromization

time limit per test
2 seconds

memory limit per test
256 megabytes

input
standard input

output
standard output

Mihahim has a string s. He wants to delete exactly one character from it so that the resulting string would be a palindrome. Determine
if he can do it, and if he can, what character should be deleted.

Input

The input contains a string s of length (2 ≤ |s| ≤ 200000),
consisting of lowercase Latin letters.

Output

If the solution exists, output «YES» (without quotes) in the first line. Then in the second line output a single integer x —
the number of the character that should be removed from s so that the resulting string would be a palindrome. The characters in the
string are numbered from 1. If there are several possible solutions, output any of them.

If the solution doesn't exist, output «NO» (without quotes).

Examples

input
evertree


output
YES
2


input
emerald


output
NO


input
aa


output
YES
2


题意:给出一个字符串,判断能否删除一个字符使它变成回文串。

思路:正反预处理字符串的哈希值用数组存下前缀和,枚举要消掉的字符即可。

# include <bits/stdc++.h>
using namespace std;

typedef unsigned long long ull;
const int N = 2e5+3;
char s
;
ull a
, b
,mod = 1e9+7;
int main()
{
int len, f=0;
ull pow=1, seed=23;
scanf("%s",s+1);
len = strlen(s+1);
for(int i=1; i<=len; ++i)
{
a[i] = (a[i-1] + s[i]*pow)%mod;
pow = (pow*seed)%mod;
}
pow = 1;
for(int i=len; i>=1; --i)
{
b[i] = (b[i+1] + s[i]*pow)%mod;
pow = (pow*seed)%mod;
}
for(int i=1; i<=len; ++i)
{
ull l = ((a[i-1]*seed)%mod + (a[len]-a[i]+mod)%mod)%mod;
ull r = ((b[i+1]*seed)%mod + (b[1]-b[i]+mod)%mod)%mod;
if(l==r)
{
f = 1;
printf("YES\n%d\n",i);
break;
}
}
if(!f) puts("NO");
return 0;
}


4000
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: