传送门

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;
}

最新文章

  1. webuploader上传文件,图片
  2. PHP修改表格(增删改)
  3. Android开发规范——命名 (转)
  4. SWD模式连接与注意事项
  5. javascript 把字符串转换为对象
  6. PHP 文件迭代器
  7. 使用截图方式将Excel导出为PNG图片的不可行性
  8. Centos7.0挂载优盘安装jdk1.7和tomcat7.0
  9. 在安全层面,企业如何获得更好的投资回报率 ROI?
  10. convertView
  11. linux proxy
  12. redis源码分析之发布订阅(pub/sub)
  13. java非阻塞IO(NIO)流程
  14. poj 3070 &amp;&amp; nyoj 148 矩阵快速幂
  15. spring boot集成websocket实现聊天功能和监控功能
  16. spring cloud(学习笔记)微服务启动错误(1)
  17. Python安装、卸载第三方模块
  18. Confluence 6 文档主题合并问答
  19. autocad视图汇报,像ppt那样汇报
  20. 51nod 1086 背包问题 V2

热门文章

  1. Layui图标
  2. linux下crontab安装和使用(定时任务)
  3. pycharm支持react
  4. 如何利用神经网络和Python生成指定模式的密码
  5. TextView设置成仅仅读
  6. 数学之路-python计算实战(21)-机器视觉-拉普拉斯线性滤波
  7. jqury-validate表单验证
  8. ios打包静态库
  9. 用JAVA生成老电影海报
  10. iOS 特定图片的button的旋转动画