传送门

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. Flask基础(3):session、flash、特殊装饰器、蓝图、路由正则匹配、上下文管理 & flask-session

    Session: Flask 默认将 session 以加密的形式放到了浏览器的 cookie 中 Flask 的 session 就是一个字典,字典有什么方法 session 就有什么方法 flas ...

  2. Codeforces913E. Logical Expression

    现有串x=11110000,y=11001100,z=10101010,通过这三个串只用与或非三种操作到达给定的串,优先级非>或>与,可以加括号,问表达式最短的里面字典序最小的是谁,有&l ...

  3. 「CodePlus 2017 12 月赛」火锅盛宴

    n<=100000种食物,给每个食物煮熟时间,有q<=500000个操作:在某时刻插入某个食物:查询熟食中编号最小的并删除之:查询是否有编号为id的食物,如果有查询是否有编号为id的熟食, ...

  4. string数据类型操作【四】

    keys *    用于查找所有的key值 exists mykey     #判断该键是否存在,存在返回1,否则返回0. del mykey        删除键(存在就删除返回1,不存在返回为0) ...

  5. 2017-10-02-afternoon

    T1 最大值(max) Time Limit:1000ms   Memory Limit:128MB 题目描述 LYK有一本书,上面有很多有趣的OI问题.今天LYK看到了这么一道题目: 这里有一个长度 ...

  6. Servlet基础教程:tutorialspoint-servlet

    来自turorialspoint的Servlet基础教程(英文),官网:https://www.tutorialspoint.com/servlets/index.htm 这个教程在国内已经被翻译成中 ...

  7. 【c++】【转】c++中的explicit关键字

    http://www.cnblogs.com/chio/archive/2007/09/17/895263.html c++中的explicit关键字用来修饰类的构造函数,表明该构造函数是显式(调用) ...

  8. Python学习系列之面向对象

    概述 一.Python编程方式 面向过程编程:根据业务逻辑从上到下磊代码 面向函数编程:将某功能代码封装到函数中,将来直接调用即可,无需重新写 面向对象编程:对函数进行分类.封装 二.面向过程编程 w ...

  9. JAVA 流程控制之选择语句

    在程序设计时,有三种基本技术可以改变程序的流程控制: 调用方法: 选择: 循环. 在这里,我们主要来讲讲选择语句. JAVA中的选择语句与C语言中的基本相同,包括: if 语句: if/else 语句 ...

  10. [Debug] Inspect and Style an Element in DevTools that Normally Disappears when Inactive

    It’s handy to inspect an element in your browser’s DevTools when you need to experiment or tweak it’ ...