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).
Example
Input
evertree
Output
YES
2
Input
emerald
Output
NO
Input
aa
Output
YES
2
题意:判断是不是回文串,如果不是问去掉一个字母之后能不能构成回文,这个去掉的字母在什么位置。
解题思路:遇到第一个不相等的就先从前面略过比较后面的,再考虑从后面略过,选择一个需要去掉个数最少的,如果这个需要去掉个数等于1或者等于0,输出YES。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include <algorithm>
using namespace std;
char a[1000005];
int main()
{
while(gets(a))
{
int len=strlen(a);
int sum=0;
int p=0,q=len-1;
int k;
while(p<=q)
{
if(a[p]==a[q])
{
p++;q--;
}
else
{
sum++;
int sum1=0,sum2=0;
int r=p,t=q-1;
while(r<=t)
{
if(a[r]==a[t])
{
r++;t--;
}
else
{
sum1++;break;
}
}
r=p+1;t=q;
while(r<=t)
{
if(a[r]==a[t])
{
r++;t--;
}
else
{
sum2++;break;
}
}
sum+=min(sum1,sum2);
if(sum1>sum2)
k=p;
else
k=q;
break;
}
}
if(sum==0)
{
printf("YES\n");
printf("%d\n",len/2+1);
}
if(sum==1)
{
printf("YES\n");
printf("%d\n",k+1);
}
if(sum==2)
printf("NO\n");
}
return 0;
}