传送门

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. Linux下汇编语言学习笔记66 ---

    这是17年暑假学习Linux汇编语言的笔记记录,参考书目为清华大学出版社 Jeff Duntemann著 梁晓辉译<汇编语言基于Linux环境>的书,喜欢看原版书的同学可以看<Ass ...

  2. Letter Combinations of a Phone Number(带for循环的DFS,组合问题,递归总结)

    Given a digit string, return all possible letter combinations that the number could represent. A map ...

  3. AtCoder Grand Contest 012 B Splatter Painting(记忆化搜索)

    题意: 给一个包含N个顶点,M条边,无自环和重边的简单无向图,初始每个点颜色都为0,每条边的长度为1,连接着ai,bi两个节点.经过若干个操作, 每次将与某个点vi距离不超过di的所有点染成某种颜色c ...

  4. Ubuntu 16.04安装Ubuntu After Install工具实现常用软件批量安装

    这个软件集成了常用且好用的软件,且只需要选择需要的软件之后自动安装好,不需要额外设置. 安装: sudo add-apt-repository ppa:thefanclub/ubuntu-after- ...

  5. openstack setup demo Compute service

    本文包含以下部分 Compute service overview Install and configure controller node Prerequisites Install and co ...

  6. DELPHI、FLASH、AS3、FLEX使用Protobuf(google Protocol Buffers)的具体方法

    最近因为工作需要,需要在不同的开发环境中应用Protobuf,特此,我专门研究了一下.为了防止自己忘记这些事情,现在记录在这里!需要的朋友可以借鉴一些,因为这些东西在GOOGLE和百度上搜索起来真的很 ...

  7. 【Linux多线程】同步与互斥的区别

    同步与互斥这两个概念经常被混淆,所以在这里说一下它们的区别. 一.同步与互斥的区别 1. 同步 同步,又称直接制约关系,是指多个线程(或进程)为了合作完成任务,必须严格按照规定的 某种先后次序来运行. ...

  8. iOS单例设计模式具体解说(单例设计模式不断完好的过程)

    在iOS中有非常多的设计模式,有一本书<Elements of Reusable Object-Oriented Software>(中文名字为<设计模式>)讲述了23种软件设 ...

  9. HDU 1226 超级password

    跟POJ 1465 multiple 类是.仅仅只是多了2个条件,长度不能超过500.还有就是 可能不是十进制. bfs+同余定理,就是用 mod 来判重. G++ 15ms 每次枚举一位,然后记录下 ...

  10. 三分钟迁移Spring boot工程到Serverless

    前言 Spring Boot已成为当今最流行的Java后端开发框架,典型的应用方式是在云上购买一台虚拟机,每天24小时在上面运行Java程序,在这种情况下,用户必须维护自己的虚拟机环境,而且按照包月包 ...