K - Palindromization Gym - 100971K (回文)

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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值