2017 Multi-University Training Contest

来源:互联网 发布:mac怎么下载千牛 编辑:程序博客网 时间:2024/06/16 01:26

Kirinriki

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 613    Accepted Submission(s): 236


Problem Description
We define the distance of two strings A and B with same length n is
disA,B=i=0n1|AiBn1i|
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
T100
0m5000
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
15abcdefedcb
 

Sample Output
5
Hint
[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
 

题意

给出字符串s,寻找其两个长度相同且不重叠的子串,满足其每位的ASCII码差值之和不大于m,且长度最长

思路

因为字符串长度很小,故我们可以枚举其每一个前缀与每一个后缀,

然后在枚举的子串内利用尺取法将区间等分,

利用差值之和不大于m 的条件双指针同时遍历两个区间,更新最大值即可


/* ***********************************************┆  ┏┓   ┏┓ ┆┆┏┛┻━━━┛┻┓ ┆┆┃       ┃ ┆┆┃   ━   ┃ ┆┆┃ ┳┛ ┗┳ ┃ ┆┆┃       ┃ ┆┆┃   ┻   ┃ ┆┆┗━┓ 马 ┏━┛ ┆┆  ┃ 勒 ┃  ┆      ┆  ┃ 戈 ┗━━━┓ ┆┆  ┃ 壁     ┣┓┆┆  ┃ 的草泥马  ┏┛┆┆  ┗┓┓┏━┳┓┏┛ ┆┆   ┃┫┫ ┃┫┫ ┆┆   ┗┻┛ ┗┻┛ ┆************************************************ */#include <iostream>#include <set>#include <map>#include <stack>#include <cmath>#include <queue>#include <cstdio>#include <bitset>#include <string>#include <vector>#include <iomanip>#include <cstring>#include <algorithm>#include <functional>#define inf 0x3f3f3f3f#define debug(x) cout<<"---"<<x<<"---"<<endltypedef long long ll;using namespace std;char str[20500];int m, len;int solve(){    int ans = 0;    for (int i = len; i >= 1; i--)    {        int cnt = i / 2 - 1;        int lz = 0, M = 0, first = 0;        for (int j = 0; j <= cnt; j++)        {            M += abs(str[j + 1] - str[i - j]);            if (M > m)            {                M -= abs(str[j + 1] - str[i - j]);                M -= abs(str[first + 1] - str[i - first]);                first++;                j--;                lz--;            }            else            {                lz++;                ans = max(ans, lz);            }        }    }    return ans;}int main(){    int t;    cin >> t;    while (t--)    {        int ans = 0;        cin >> m;        cin >> str + 1;        len = strlen(str + 1);        ans = max(ans, solve());        reverse(str + 1, str + len + 1);        ans = max(ans, solve());        cout << ans << endl;    }    return 0;}/************************************************┆  ┏┓   ┏┓ ┆┆┏┛┻━━━┛┻┓ ┆┆┃       ┃ ┆┆┃   ━   ┃ ┆┆┃ ┳┛ ┗┳ ┃ ┆┆┃       ┃ ┆┆┃   ┻   ┃ ┆┆┗━┓    ┏━┛ ┆┆  ┃    ┃  ┆      ┆  ┃    ┗━━━┓ ┆┆  ┃  AC代马   ┣┓┆┆  ┃           ┏┛┆┆  ┗┓┓┏━┳┓┏┛ ┆┆   ┃┫┫ ┃┫┫ ┆┆   ┗┻┛ ┗┻┛ ┆************************************************ */


原创粉丝点击