暑假集训7.11 字符串回文暴力

#include <iostream>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <algorithm>
#include <set>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
#define MM(a,b) memset(a,b,sizeof(a));
const double eps = 1e-10;
const int  inf =0x7f7f7f7f;
const double pi=acos(-1);
int p2,q2,p1,ep,q1;
const int INF = 0x3f3f3f3f;
const int maxn = 1100;
int l;
char s[200000+10],s1[200000+10];
bool check(int pos)
{
    int m=0;
    for(int i=0;i<l;i++)
       if(i!=pos)
          s1[m++]=s[i];
    for(int i=0,j=l-1-1;i<=j;i++,j--)
         if(s1[i]!=s1[j]) return false;
    return true;
}
int main()
{
    while(~scanf("%s",s))
    {
        l=strlen(s);
        int pos=-1;
        for(int i=0,j=l-1;i<=j;i++,j--)
             if(s[i]!=s[j])
               {pos=i;break;}
        if(pos==-1) printf("YES
%d
",l/2+1);
        else if(check(pos)) printf("YES
%d
",pos+1);
        else if(check(l-1-pos)) printf("YES
%d
",l-1-pos+1);
        else printf("NO
");
    }
    return 0;
}

 题目链接:

分析:很好的一道题目啊,想到了要将字符串取反放进一个新的数组中,最后却处理的比较复杂,学习一下简洁 的解法,首先找到使得整个字符串不满足回文的字符第一次出现的位置,那么要让数组变成回文就必须处理一下这个 位置也就是删除一个,不过可以删除前面的一个,也可以删除后面的一个,究竟删除哪一个?其实也只有两种情况 暴力求一下就好

原文地址:https://www.cnblogs.com/smilesundream/p/5662072.html