Kirinriki
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 21 Accepted Submission(s): 2
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.
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
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
5Hint[0, 4] abcde [5, 9] fedcb The distance between them is abs('a' - 'b') + abs('b' - 'c') + abs('c' - 'd') + abs('d' - 'e') + abs('e' - 'f') = 5
Source
Recommend
liuyiding | We have carefully selected several similar problems for you: 6107 6106 6105 6104 6103
两个不重合的子串向中心一起延长会形成奇偶长度两种合串。
枚举一下中心向外延伸,如果和超过了阈值弹掉中心处的位置。双指针维护。
时间复杂度O(n2)
#include<cstdio>
#include<cmath>
#include<cstring>
#include<algorithm>
using namespace std;
#define ll long long
#define N 50005
int n,m,ans;
int q[N],p[N];
char s[N];
void fun(int l,int r) {
int x,y,v;
x=y=v=0;
q[y]=0;
p[y]=r-1;
y++;
while(l>=0&&r<n) {
v+=abs(s[r]-s[l]);
q[y]=v;
p[y]=r;
y++;
while(v-q[x]>m)x++;
ans=max(ans,r-p[x]);
l--,r++;
}
//printf("%d\n",ans);
}
int main() {
int T;
int i;
scanf("%d",&T);
while(T--) {
scanf("%d",&m);
scanf("%s",s);
n=strlen(s);
ans=0;
for(i=1; i<n; i++) {
fun(i-1,i);
fun(i-2,i);
}
printf("%d\n",ans);
}
return 0;
}