【Gym 100971K】Palindromization

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

从左右两边往中间移动,如果相同,l++,r--,如果不同,第一次循环i==0时,则让l++,第二次循环则让r--,最后判断不同的出现了几次。如果dif是0,l>=r代表本身就是回文,则去掉l位置的字符。

#include <cstring>
#include <cstdio>
#define N 100005
char s[N<<1];
int len,l,r,dif,at;
int main() {
    scanf("%s",s);
    len=strlen(s);
    for(int i=0;i<2;i++){
        dif=0;
        l=0,r=len-1;
        while(l<r){
            if(s[l]!=s[r]){
                dif++;
                if(i){
                    at=r;
                    r--;
                }else {
                    at=l;
                    l++;
                }
            }
            if(s[l]==s[r]){
                l++;
                r--;
            }
        }
        if(l>=r&&dif==0)at=l;
        if(dif<2){
            printf("YES\n%d\n",at+1);
            break;
        }
    }
    if(dif>1)puts("NO");
}

 

  

转载于:https://www.cnblogs.com/flipped/p/5749040.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值