您的位置:首页 > 其它

hdu6103-2017多校6&尺取法|思维-Kirinriki

2017-08-12 10:07 246 查看
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 尺取法
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: