http://acm.hdu.edu.cn/showproblem.php?pid=6103
给定一个数组,存在两个两个子串
dis+=(a[i]+b[n-i-1]) 要求dis小于等于k,问你最长的这两个串。
这两个串向中间合,最后的结果可能是奇数串,或者是偶数串,然后枚举中间点,从中间向两边括,如果超过了就舍弃中间点。向外。
#include <bits/stdc++.h>
using namespace std;
int main()
{ //freopen("f:\\ttt\\1008.txt","r",stdin);
//freopen("f:\\ttt\\out1.txt","w",stdout);
int t,k,all,max1;
string s;
cin>>t;
while(t--){
cin>>k;
cin>>s;
all=0;
max1=0;
int len=s.length();
for(int i=0;i<len;i++){
int pl=i;
int pr=i;
int l=i;
int r=i;
all=0;
while(r<len&&l>=0){
all+=abs(s[r]-s[l]);
while(all>k&&pr<=r&&pl>=l){
all-=abs((s[pr]-s[pl]));
pr++;pl--;
//cout<<max1<<":"<<endl;
}
if(pr==i)
max1=max(max1,r-pr);
else
max1=max(max1,r-pr+1);
r++;l--;
}
}//合串为偶数
for(int i=0;i<len;i++){
int pl=i-1;
int pr=i;
int l=i-1;
int r=i;
all=0;
while(r<len&&l>=0){
all+=abs(s[r]-s[l]);
while(all>k&&pr<=r&&pl>=l){
all-=abs((s[pr]-s[pl]));
pr++;pl--;
//cout<<":"<<max1<<endl;
}
max1=max(max1,r-pr+1);
r++;l--;
}
}//合串为偶数
printf("%d\n",max1);
}
return 0;
}
2 尺取法