【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");
}
复制代码

 

  

posted @   水郁  阅读(506)  评论(0编辑  收藏  举报
编辑推荐:
· 智能桌面机器人:用.NET IoT库控制舵机并多方法播放表情
· Linux glibc自带哈希表的用例及性能测试
· 深入理解 Mybatis 分库分表执行原理
· 如何打造一个高并发系统?
· .NET Core GC压缩(compact_phase)底层原理浅谈
阅读排行:
· 新年开篇:在本地部署DeepSeek大模型实现联网增强的AI应用
· DeepSeek火爆全网,官网宕机?本地部署一个随便玩「LLM探索」
· Janus Pro:DeepSeek 开源革新,多模态 AI 的未来
· 上周热点回顾(1.20-1.26)
· 【译】.NET 升级助手现在支持升级到集中式包管理
欢迎这位怪蜀黍来到《【Gym 100971K】Palindromization - 水郁 - 博客园》
点击右上角即可分享
微信分享提示