hdu6103Kirinriki(第六场尺取法取区间最大)
来源:互联网 发布:阿里云数据价格 编辑:程序博客网 时间:2024/06/05 05:06
Kirinriki
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 1822 Accepted Submission(s): 749
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.
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
Each character in the string is lowercase letter,
Output
For each test case output one interge denotes the answer : the maximum length of the substring.
Sample Input
15abcdefedcb
Sample Output
5Hint[0, 4] abcde[5, 9] fedcbThe distance between them is abs('a' - 'b') + abs('b' - 'c') + abs('c' - 'd') + abs('d' - 'e') + abs('e' - 'f') = 5
Source
2017 Multi-University Training Contest - Team 6
求最大字串,使得交叉相减绝对值和最大且不超过m的字串长度
处理区间最大问题用尺取法,从a开始枚举,如果枚举的res值超过m,则退回到开头,下次该区间跳过枚举,枚举到最后一个字符时,枚举起始地址+1。
下面举例说明:
1
2
abcd
####0-1=1
####0-2=3
****0-2=1
l ---->0
^^^^0-2=-1
####0-2=1
####0-3=4
****0-3=1
l ---->0
^^^^0-3=-2
####0-3=1
####1-2=2
2
2
abcd
####0-1=1
####0-2=3
****0-2=1
l ---->0
^^^^0-2=-1
####0-2=1
####0-3=4
****0-3=1
l ---->0
^^^^0-3=-2
####0-3=1
####1-2=2
2
带#号的为第一次枚举,*号为超出了m,去掉当前枚举值,^为继续后退,得到新的起始值,继续枚举
最后直到枚举到1-2结束,输出ans=2
#include <stdio.h>#include <cstring>#include <algorithm>#include <iostream>using namespace std;const int maxn=20005;char s[maxn];int m,len;int main(){ int t; scanf("%d",&t); while(t--) { scanf("%d",&m); scanf("%s",s); len=strlen(s); int ans=0; for(int i=0;i<len;i++) { int t=0,res=0,l=0; for(int j=0;j<(i+1)/2;j++) { res+=abs(s[j]-s[i-j]); //printf("####%d-%d=%d\n",j,i-j,ans); if(res<=m) { t++; ans=max(t,ans); } else { res-=abs(s[j]-s[i-j]); //printf("****%d-%d=%d\n",j,i-j,ans); res-=abs(s[l]-s[i-l]); //printf("l ---->%d\n",l); //printf("^^^^%d-%d=%d\n",j,i-j,ans); l++,t--,j--; } } } reverse(s,s+len); //cout<<s<<endl; for(int i=0;i<len;i++) { int t=0,res=0,l=0; for(int j=0;j<(i+1)/2;j++) { res+=abs(s[j]-s[i-j]); if(res<=m) { t++; ans=max(t,ans); } else { res-=abs(s[j]-s[i-j]); res-=abs(s[l]-s[i-l]); l++,t--,j--; } } } printf("%d\n",ans); }}
阅读全文
0 0
- hdu6103Kirinriki(第六场尺取法取区间最大)
- HDU6103Kirinriki(尺取)
- hdu6103Kirinriki(尺取法)
- hdu 5358 First One (2015多校第六场第6题)尺取法枚举区间和
- hdu5358 First One 尺取法 多校联合第六场
- 尺取法 枚举区间
- HDU 6103 Kirinriki (字符串翻转 尺取法 17多校第六场)
- HDU 6013 Kirinriki (字符串反转+尺取法 17多校赛第六场)
- 山东省第六届 ACM 省赛 Stars (尺取法)
- 区间价值?hihocoder1483《二分!!+尺取法》
- 51NOD1495 中国好区间 【尺取法】
- hdu6103 尺取 双指针 2017多校第六场1008
- (多校第六场1001)HDU5793 A Boring Question(逆元,等比数列和取模)
- 计蒜客2017 第五场 UCloud 的安全秘钥(中等) 尺取法
- 2017百度之星初赛(B) 1006 小小粉丝度度熊(区间合并+尺取法)
- 51nod 1495中国好区间 && qduoj78 阿尔法狗的难题 (尺取法)
- poj3061Subsequence(尺取法)
- 尺取法(1)
- 最佳完美匹配性质1poj3565-边上的性质
- NOIP1999提高组·····邮票面值设计
- EXCEL的小妙用
- 《笨办法学python》加分习题13——我的答案
- 数据库分库分表中间件 Sharding-JDBC 源码分析 —— SQL 解析(四)之插入SQL解析
- hdu6103Kirinriki(第六场尺取法取区间最大)
- [bzoj 4809]皇后 (dfs)
- HDU1176 免费馅饼 DP(两种方法)
- PL/SQL Developer连接本地Oracle 11g 64位数据库
- bzoj1455: 罗马游戏
- CentOS下使用rpm 几个常用命令
- PyInstaller(windows开发,打包python软件为exe)使用教程
- Springboot热部署
- 计算机视觉相关网站