2017 Multi-University Training Contest - Team 6:1008&hdu6103、Kirinriki

题目:

Problem Description
We define the distance of two strings A and B with same length n is
disA,B=i=0n1|AiBn1i|
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
T100
0m5000
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
题意:给出一字符串,求出两个子串长度相等且满足条件 disA,B=i=0n1|AiBn1i| 的最大长度

思路:利用尺取法,遍历字符串S的每一个字符,从子串A的末端与子串B的起始端开始计算dis_AB(i-1,i+1)&&((i,i+1)||(i-1,i)->看遍历起始位置),最后查找最长的长度~说不太清,看代码就行,很明了~感谢f_zyj的指点~

CODE:

#include<bits/stdc++.h>
using namespace std;
char s[5005];
int m,ans,cnt,a[2502];
void judge(){
        int sum=0,l=0,r=0;
        while(r<cnt){
                sum+=a[r++];
                while(sum>m) sum-=a[l++];
                ans=max(ans,r-l);
        }
}
void solve(){   //分别计算中间空余偶数个或奇数个字符的情况
        int i,l,r,len=strlen(s);
        ans=0;
        for(i=1;i<len;i++){
                cnt=0;
                l=i-1,r=i+1;
                while(l>=0&&r<len) a[cnt++]=abs(s[l--]-s[r++]);
                judge();
        }
        for(i=1;i<len;i++){
                cnt=0;
                l=i-1,r=i;
                while(l>=0&&r<len) a[cnt++]=abs(s[l--]-s[r++]);
                judge();
        }
        /*for(i=0;i<len;i++){
                cnt=0;
                l=i,r=i+1;
                while(l>=0&&r<len) a[cnt++]=abs(s[l--]-s[r++]);
                judge();
        }*/
        printf("%d\n",ans);
}
int main(){
        int t;
        scanf("%d",&t);
        while(t--){
                scanf("%d%s",&m,s);
                solve();
        }
        return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值