HDU 6103 Kirinriki (思维 双指针)
Kirinriki
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 2169 Accepted Submission(s): 879
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
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
Source
【题意】给你一个字符串,找到两个不相交的相同长度的子串,使得|Ai−Bn−1−i|求和小于等于m,求子串最大长度。
【分析】可以枚举前缀,然后双指针,这种做法需要翻转一下再来一次,不懂得可以看看这个样例abcdefghi 答案是 def ghi,然后还可以枚举对称轴,这个不需要翻转。
#include <bits/stdc++.h> #define inf 0x3f3f3f3f #define met(a,b) memset(a,b,sizeof a) #define pb push_back #define mp make_pair #define rep(i,l,r) for(int i=(l);i<=(r);++i) #define inf 0x3f3f3f3f using namespace std; typedef long long ll; const int N = 5e3+50;; const int M = 255; const int mod = 998244353; const int mo=123; const double pi= acos(-1.0); typedef pair<int,int>pii; typedef pair<ll,int>P; int n,m,ans; char str[N]; void solve(){ for(int len=2;len<=n;len++){ for(int l=1,r=1,res=0;r<=len/2;r++){ res+=abs(str[r]-str[len-r+1]); while(res>m){ res-=abs(str[l]-str[len-l+1]); l++; } ans=max(ans,r-l+1); } } } int main(){ int T; scanf("%d",&T); while(T--){ scanf("%d",&m); scanf("%s",str+1); n=strlen(str+1); ans=0; solve(); reverse(str+1,str+1+n); solve(); printf("%d\n",ans); } return 0; }