牛客训练四:Applese 的回文串(思维)

题目链接:传送门

思路:插入不同的字符与删除不同的字符相同,所以每次判断到不同位置时将这个字符删除然后判断是否为回文串即可,

如果一开始就是回文串,那么答案也是yes。

#include<iostream>
#include<cstdio>
#include<string>
using namespace std;
string ss,s1,s2;
int pos1,pos2;
bool pd(string str)
{
    int i,j,len=str.length();
    for(i=0,j=len-1;i<j;i++,j--)
    if(str[i]!=str[j]){
        pos1=i;pos2=j;
        return false;
    }
    return true;
}
void change()
{
    s1="";s2="";
    for(int i=0;i<ss.length();i++)
    {
        if(i!=pos1) s1+=ss[i];
        if(i!=pos2) s2+=ss[i];
    }
}
int main(void)
{
    while(cin>>ss){
        if(pd(ss)) printf("Yes\n");
        else{
            change();
            if(pd(s1)||pd(s2)) printf("Yes\n");
            else printf("No\n");
        }
    }
    return 0;
}
View Code

猜你喜欢

转载自www.cnblogs.com/2018zxy/p/10335829.html