您的位置:首页 > 大数据 > 人工智能

2017 Multi-University Training Contest - Team 6 1008 Kirinriki

2017-08-10 22:25 453 查看

Kirinriki

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

[align=left]Problem Description[/align]

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.

 

[align=left]Input[/align]

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

 

[align=left]Output[/align]

For each test case output one interge denotes the answer : the maximum length of the substring.

 

[align=left]Sample Input[/align]

1
5
abcdefedcb

 

[align=left]Sample Output[/align]

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

 

题意

给出字符串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<<"---"<<endl
typedef 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代马   ┣┓┆
┆  ┃           ┏┛┆
┆  ┗┓┓┏━┳┓┏┛ ┆
┆   ┃┫┫ ┃┫┫ ┆
┆   ┗┻┛ ┗┻┛ ┆
************************************************ */
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐