Pig-Palindromic(思维)

版权声明:本文章未经博主允许不得转载 https://blog.csdn.net/qq_42217376/article/details/89553436

链接:https://ac.nowcoder.com/acm/contest/877/E
来源:牛客网——黑龙江大学程序设计竞赛(重现赛)

  题意是说给我们一个字符串,我们将一个字符串的大学字母全部变成小写,小写字母全部变成大写.然后将这个字符串倒置.最终的字符串是刚开始的字符串则满足题意.我们最终的任务是要寻找某个字符串中满足上述的条件的最长子串.我们可以知道如果满足上述条件,那么最中间的两个字母一定是一样的,并且一个是大写一个是小写.从这里我们就可以找到切入点去写代码.我们枚举相邻的两个字母,看看这两个字母能否向外拓展,从而找到最长的子串.

#include<cstdio>
#include<iostream>
#include<queue>
#include<cstring>
#include<string>
#include<algorithm>
#include<vector>
using namespace std;

typedef long long LL;
const int Max_n=5e3+10;
char a[Max_n];

int get_ans(int n,int l,int r){
    int ans=0;
    while(l>=0&&r<n){
        if(a[l]+32==a[r]||a[r]+32==a[l]){
            ans+=2;
            l--;r++;
        }

        else
            break;
    }

    return ans;
}
int main(){
    scanf("%s",a);
    int n=strlen(a);
    int ans=0;
    for(int i=0;i<n-1;i++)
        ans=max(ans,get_ans(n,i,i+1));
    printf("%d\n",ans);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_42217376/article/details/89553436
pig