Kirinriki
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1792 Accepted Submission(s): 735
Problem Description
We define the distance of two strings A and B with same length n is
dis
A,B
=∑
i=0
n−1
|A
i
−B
n−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.
dis
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
5
abcdefedcb
Sample Output
5
Hint[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
题意:输入m和一个串,要在串中找到两个不会重叠长度相等并且长度尽量长的字串,使得定义的距离小于等于m;
思路:从中心向两端枚举,学弟画了一张图(抱大腿啊):
思路:从中心向两端枚举,学弟画了一张图(抱大腿啊):
代码:
#include<cstdio>
#include<cstring>
#include<iostream>
#include<cmath>
#include<algorithm>
using namespace std;
string s1,s2;
int s[5050],m;
int sum[5050];
int get_len(int n) //尺取
{
sum[0]=0;
for(int i=1; i<=n; i++)
sum[i]=sum[i-1]+s[i];
int maxx=0,l=1,r=1;
while(l<=r&&l<=n&&r<=n)
{
if(sum[r]-sum[l-1]<=m)
{
maxx=max(maxx,r-l+1);
r++;
}
else
{
l++;
if(l>r) //没加这句,wa晕
r=l;
}
}
return maxx;
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
memset(s,0,sizeof(s));
int max_len=0;
scanf("%d",&m);
cin>>s1;
s2=s1;
reverse(s2.begin(),s2.end());
for(int i=0; i<(int)s1.size()-1; i++)
{
int len=(s2.size()-i)/2;
if(max_len>=len) break;
for(int j=0; j<len; j++)
s[j+1]=abs(s1[i+j]-s2[j]);
max_len=max(max_len,get_len(len));
}
for(int i=0; i<(int)s2.size()-1; i++)
{
int len=(s1.size()-i)/2;
if(max_len>=len) break;
for(int j=0; j<len; j++)
s[j+1]=abs(s2[i+j]-s1[j]);
max_len=max(max_len,get_len(len));
}
printf("%d\n",max_len);
}
return 0;
}