HDU 6103 Kirinriki

好久没更博客了,最近好懒….多校打了六场,每场都是稳定输出第一题…然后队友第二题…然后剩下四个多小时死磕第三题…..要好好了努力了

Kirinriki

Problem Description
We define the distance of two strings A and B with same length n is
disA,B=∑i=0n−1|Ai−Bn−1−i|
The difference between the two characters is defined as the difference in ASCII.
You should find the maximum length of two non-overlapping substrings in given string S, and the distance between them are less then or equal to m.

Input
The first line of the input gives the number of test cases T; T test cases follow.
Each case begins with one line with one integers m : the limit distance of substring.
Then a string S follow.

Limits
T≤100
0≤m≤5000
Each character in the string is lowercase letter, 2≤|S|≤5000
∑|S|≤20000

Output
For each test case output one interge denotes the answer : the maximum length of the substring.

Sample Input
1
5
abcdefedcb

Sample Output
5
问题就是给你一个串,让你求两个连续的子串,使其ASCII的差的绝对值之和不大于m的情况下,使子串最长
这道题用枚举中心点的方法, 依次向外扩展,直到不能扩展为止,取长度最大值,因为会遇到奇偶串的情况,所以要枚举两次

#include<bits/stdc++.h>
using namespace std;
using LL =int64_t;
const int INF=0x3f3f3f3f;
const int maxn=5005;
int m,sum=0,len;
char s[maxn];

void work(int x,int y) {
    int l=0,r=0,cnt=0;
    while(y+r<len&&x-r>=0) {//s[x-r]~s[x-l]和s[y+l]~s[y+r]比较
        if(cnt+abs(s[y+r]-s[x-r])<=m) {//先扩展r,再扩展l
            cnt+=abs(s[y+r]-s[x-r]);
            r++;
            sum=max(sum,r-l);
        }
        else {
            cnt-=abs(s[y+l]-s[x-l]);
            l++;
        }
    }
    return ;
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);
    int T;
    cin>>T;
    while(T--){
        sum=0;
        cin>>m>>s;
        len=strlen(s);
        for(int i=0;i<len;i++) {//枚举两个子串的中心
            work(i,i+1);//因为存在奇偶长度的子串,所以要枚举两次
            work(i-1,i+1);
        }
        cout<<sum<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值