传送门

Kirinriki

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)

Total Submission(s): 1012    Accepted Submission(s): 400


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
 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:  6107 6106 6105 6104 6103 
 

Statistic | Submit | Discuss | Note

题意:

已知字符串S,要求不重叠的最长子串长度,并满足两子串距离最大不超过m

/*
思路:由于字符串很短,所以可以枚举前缀和后缀
在枚举的子串内采用尺取法将区间等分,利用sum不大于m的条件双指针同时遍历两个区间,更新最大值即可 */
#include<bits/stdc++.h>
#include<iostream>
#include<stdio.h>
using namespace std; const int maxn=5e3+10;
int m,nl,ans;//nl表示字符串长度,ans代表最后返回的子串长度
char s[maxn]; void solve()
{
for(int i=2;i<=nl;i++)//i表示从总串中取出的子串长度
{
int o=i/2;
int l=0,n=0,sum=0;
for(int j=0;j<o;j++)
{
sum+=abs(s[j]-s[i-j-1]);
if(sum<=m)n++,ans=max(ans,n);
else
{
sum-=abs(s[l]-s[i-l-1]);
sum-=abs(s[j]-s[i-j-1]);
l++;
j--;
n--;
}
}
}
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d %s",&m,s);
nl=strlen(s);
ans=0;
solve();
reverse(s,s+nl);
solve();
printf("%d\n",ans); }
return 0;
}

2017杭电多校第六场1008 Kirinriki的更多相关文章

  1. 2017杭电多校第六场1011Classes

    传送门 Classes Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Tota ...

  2. 2017杭电多校第六场03Inversion

    传送门 Inversion Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) To ...

  3. 2017杭电多校第五场11Rikka with Competition

    Rikka with Competition Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/O ...

  4. 2017杭电多校第五场Rikka with Subset

    Rikka with Subset Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others ...

  5. 2018杭电多校第六场1009(DFS,思维)

    #include<bits/stdc++.h>using namespace std;int a[100010];char s[20];int zhiren[100010];vector& ...

  6. 2017杭电多校第七场1011Kolakoski

    Kolakoski Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others) Tota ...

  7. 2017杭电多校第七场1005Euler theorem

    Euler theorem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 524288/524288 K (Java/Others) ...

  8. [2019杭电多校第六场][hdu6641]TDL

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6641 题意为求出最小的n,满足(f(n,m)-n)^n=k,其中f(n,m)为第m大的x,其中x满足g ...

  9. [2019杭电多校第六场][hdu6638]Snowy Smile(维护区间最大子段和)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6638 题意为在一个平面上任意选择一个长方形,使得长方形内点权和最大. 因为长方形可以任意选择,所以上下 ...

随机推荐

  1. python基础之-字符串

    字符模块:strstr.strip():去掉字符串前后空格str.lstrip():去掉字符串左侧空格str.rstrip():去掉字符串右侧空格str.encode():将字符串编码为二进制str. ...

  2. Codeforces 651B Beautiful Paintings【贪心】

    题意: 给定序列,重新排序,使严格上升的子序列最多.求这些子序列总长度. 分析: 贪心,统计每个元素出现次数,每次从剩余的小的开始抽到大的,直到不再剩余元素. 代码: #include<iost ...

  3. oracle删除表前先判断表是否存在

     DECLARE  numbe NUMBER;BEGIN  SELECT COUNT(1)    INTO numbe    FROM USER_TABLES   WHERE TABLE_NAME = ...

  4. pycharm内存不足时如何修改设置?

    Help->Find Action->(type "VM Options")->(Click)"Edit Custom VM Options" ...

  5. NYOJ 题目42 一笔画问题(欧拉图)

    一笔画问题 时间限制:3000 ms  |  内存限制:65535 KB 难度:4 描写叙述 zyc从小就比較喜欢玩一些小游戏.当中就包含画一笔画.他想请你帮他写一个程序.推断一个图是否可以用一笔画下 ...

  6. 条款五:对应的new和delete要采用相同的形式

    string *stringarray = new string[100]; ... delete stringarray; 上述程序的运行情况将是不可预测的.至少,stringarray指向的100 ...

  7. Linux命令输出头(标题)、输出结果排序技巧

    原文:http://blog.csdn.net/hongweigg/article/details/65446007 ----------------------------------------- ...

  8. angular 的ui.router 定义不同的state 对应相同的url

    Angular UI Router: Different states with same URL? The landing page of my app has two states: home-p ...

  9. Python 出现 can't use a string pattern on a bytes-like object

    Python 出现 can't use a string pattern on a bytes-like object 学习了:https://www.cnblogs.com/andrewleeeee ...

  10. Spring Boot中微信全局token的缓存实现

    为什么要缓存token? 这里的token指的是微信JSAPI中基础支持的ACCESS_TOKEN,并非网页授权ACCESS_TOKEN.网页授权Token每天的调用次数没有限制,不需要缓存. 接口 ...