HDU 6103 Kirinriki

来源:互联网 发布:互联网金融平台源码 编辑:程序博客网 时间:2024/06/06 00:26

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

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;}
原创粉丝点击