HDU 6103 Kirinriki

来源:互联网 发布:高瓴资本集团大数据 编辑:程序博客网 时间:2024/06/06 02:54

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

Recommend
liuyiding | We have carefully selected several similar problems for you: 6119 6118 6117 6116 6115

題意:給你一個字符串,再給你一個數字n, 你可以在原字符串中截取兩段長度都為n的字符串稱為substring,要求找出距離最小的兩個字符串。
對於距離distance是這樣定義的:
disA,B=∑i=0n−1|Ai−(Bn−1−i)|

題目中也給出了例子

[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

#include<iostream>#include<cstdio>#include<cstring>#include<string>#include<algorithm>#include<cmath>using namespace std;//不要小瞧人生啊#define rep(i,a,b) for(int i=a;i<(b);++i)const int N = 1e4 + 10;int T, m, n;char s[N];int main() {    scanf("%d", &T);    rep(i, 0, T)     {        scanf("%d%s", &m, s);        int Mx = 0;        n = strlen(s);        rep(i, 0, n)         {            for (int l = i, r = i, pl = l, pr = r, dif = 0; 0 <= l&&r<n; ++r, --l)             {                dif += abs((int)s[r] - s[l]);                while (pr <= r&&dif>m)                     dif -= abs((int)s[pr++] - s[pl--]);                Mx = max(Mx, r - pr + (pr != i));            }        }        rep(i, 1, n)         {            for (int l = i - 1, r = i, pl = l, pr = r, dif = 0; 0 <= l&&r<n; ++r, --l)             {                dif += abs((int)s[r] - s[l]);                while (pr <= r&&dif>m)                     dif -= abs((int)s[pr++] - s[pl--]);                Mx = max(Mx, r - pr + 1);            }        }        printf("%d\n", Mx);    }    return 0;}
原创粉丝点击