hdu6103-Kirinriki (2017杭电多校联赛第6场)
2017-08-11 10:13
465 查看
Kirinriki
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 796 Accepted Submission(s): 306
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.
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
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
2017 Multi-University Training Contest - Team 6
题目大意:给你一个计算两个相同长度的计算公式,每两个相同长度的字符串会得到一个值,现在给你一个字符串,然后给你一个值,询问在这个字符串中得到这个值的两个字符串的最大距离差。
解题思路:我们枚举每个位置,然后往两边寻找,暴力枚举每个位置得到最大值。
ac代码如下:
#include<cstdio> #include<cmath> #include<cstring> #include<algorithm> using namespace std; #define ll long long const int N=50005; int n,m,ans; int q ,p ; char s ; void cal(int l,int r){ int x,y,v; x=y=v=0; q[y]=0; p[y]=r-1; y++; while(l>=0&&r<n){ v+=abs(s[r]-s[l]); q[y]=v; p[y]=r; y++; while(v-q[x]>m)x++; ans=max(ans,r-p[x]); l--,r++; } } int main(){ int T; int i; scanf("%d",&T); while(T--){ scanf("%d",&m); scanf("%s",s); n=strlen(s); ans=0; for(i=1;i<n;i++){ cal(i-1,i); cal(i-2,i); } printf("%d\n",ans); } return 0; }
题目链接:点击打开链接http://acm.hdu.edu.cn/showproblem.php?pid=6103
相关文章推荐
- HDU6103 Kirinriki(尺取法,2017 HDU多校联赛 第6场)
- Hdu6103 Kirinriki(2017多校第6场)
- HDU6103---Kirinriki(2017多校联赛:滑动窗)
- 2017 Multi-University Training Contest - Team 6:1008&hdu6103、Kirinriki
- HDU6103 Kirinriki-dp+二分 - 2017多校联盟6 第8题
- 2017杭电多校第六场1008 Kirinriki
- 2017多校训练6-1008:Kirinriki(HDU6103)
- HDU6070 2017杭电多校联赛第四场-Dirt Ratio
- 2017杭电多校联赛6-Inversion-排序-hdu6098
- 2017杭电多校联赛 1001 Add More Zero(取对数)HDU 6033
- 2017 Multi-University Training Contest - Team 6 1008 Kirinriki
- HDU6103-Kirinriki
- 2017杭电多校联赛第二场-Funny Function (hdu6050)快速幂解数学方程
- Kirinriki 2017多校
- HDU6106 Classes(思路,2017 HDU多校联赛 第6场)
- 2017杭电多校联赛第二场-TrickGCD (hdu6053)莫比乌斯容斥
- 2017 杭电多校联赛第二场 1011 Regular polygon(多个点求正方形个数)POJ 2002
- (hdu6075) 2017杭电多校联赛第四场-Questionnaire 思维题
- HDU6103-Kirinriki
- 2017杭电多校联赛-Add More Zero 数学找公式