hdu 6103 Kirinriki 【尺取】


点击打开链接



题意:

       给你一个区间,定义了两个子区间的距离不能大于m ,问满足这样距离的子区间最大长度。

       区间距离dis(x,y)= abs(x1-yn)+abs(x2-yn-1).......;

题解:

       2s 5000长度,总长度20000.  

       n^2 勉强能过。

       很明显可以看出来所有满足题意的两点满足回文性质, 所以枚举每个子串的的中点, 然后依次向两边扩散,

       然后枚举的中点即可能是一个点,也可能是两个点的中间,所以推两边即可。


#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int maxn=5005;
char s[maxn];
int T,m,ed;
int Abs(int x){
    if(x<0)return -x;
    return x;
}
int solve(char *s){
    int ans=0;
    for(int i=0;i<ed;++i){
        int pre=i,sub=i+1,ret=0,cnt=0;
        int pt=i,st=i+1;
        while(pre>=0&&sub<ed){
            ret+=Abs(s[pre]-s[sub]);
            if(ret>m){
                ret-=Abs(s[pre]-s[sub]);
                if(st!=sub){
                    ret-=Abs(s[st]-s[pt]);
                    cnt--,pre++,sub--;
                }
                pt--;st++;
            }else{
                cnt++;
                ans=max(ans,cnt);
            }pre--,sub++;
        }
    }
    return ans;
}
int solve1(char *s){
    int ans=0;
    for(int i=0;i<ed;++i){
        int pre=i-1,sub=i+1,ret=0,cnt=0;
        int pt=i-1,st=i+1;
        while(pre>=0&&sub<ed){
            ret+=Abs(s[pre]-s[sub]);
            if(ret>m){
                ret-=Abs(s[pre]-s[sub]);
                if(st!=sub){
                    ret-=Abs(s[st]-s[pt]);
                    cnt--,pre++,sub--;
                }
                pt--;st++;
            }else{
                cnt++;
                ans=max(ans,cnt);
            }pre--,sub++;
        }
    }
    return ans;
}
int main(){
    scanf("%d",&T);
    while(T--){
        scanf("%d",&m);
        scanf("%s",s);
        ed=strlen(s);
        int t=solve(s);
        t=max(t,solve1(s));
        printf("%d\n",t);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值