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,问你最长的这两个串。
这两个串向中间合,最后的结果可能是奇数串,或者是偶数串,然后枚举中间点,从中间向两边括,如果超过了就舍弃中间点。向外。
2 尺取法
给定一个数组,存在两个两个子串
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 尺取法
相关文章推荐
- 【思维】2017多校训练七 HDU6121 Build a tree
- (2017多校训练第二场)HDU - 6052 To my boyfriend 思维题
- HDU6103 Kirinriki-dp+二分 - 2017多校联盟6 第8题
- 2017多校训练6-1008:Kirinriki(HDU6103)
- 2017 多校训练第二场 HDU 6045 Is Derek lying?(思维)
- Hybrid Crystals (2017多校 第八场) (思维题 视力题)
- HDU-2017 多校训练赛6-1008-Kirinriki
- hdu6047-贪心&思维-2017多校(2)-Maximum Sequence
- Hdu6103 Kirinriki(2017多校第6场)
- hdu6038-思维-2017多校(2)-Function
- HDU6038-Function-数学+思维-2017多校Team01
- HDU6103 Kirinriki(尺取法,2017 HDU多校联赛 第6场)
- HDU6103---Kirinriki(2017多校联赛:滑动窗)
- HDU6090-思维&构造&贪心&&2017多校5-Rikka with Graph
- HDU6055——多校2017
- HDU 6038 (2017 多校训练赛1 1006) Function(图论)
- 2017广东工业大学程序设计竞赛决赛 Problem E: 倒水(Water)(思维)
- 2017 多校 Function(置换群
- HDU 6055 (2017 多校训练赛2 1011)Regular polygon
- 2017 杭电多校联赛第二场 1003 Maximum Sequence(单调队列)HDU 6047