We define the distance of two strings A and B with same length n is
dis A,B =∑ i=0 n−1 |A i −B n−1−i | 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.

InputThe 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 T≤100

0≤m≤5000 0≤m≤5000

Each character in the string is lowercase letter, 2≤|S|≤5000 2≤|S|≤5000

∑|S|≤20000 ∑|S|≤20000

OutputFor 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

题意:给堵一个字符串,求最长的两个不相交字串S、T,其字符串值之差(倒序的字符之差的绝对值之和)小于M,输出这个长度。

思路:尺取法,枚举起点终点发现没法做,我们枚举S和T的对称点,然后根据对称点尺取。即每次右边界++,维护左边界,使其满足小于M。

主要是利用了此题中,字符串之差是首尾倒序做差,我们我们可以这样处理。  有点像求回文串一样。

#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
char c[]; int T,N,M,ans;
void solve()
{
rep(i,,N){
int L=,R=,tmp=;
while(i+R+<=N&&i-R->=){
R++; tmp+=abs(c[i+R]-c[i-R]);
while(tmp>M) tmp-=abs(c[i+L]-c[i-L]),L++;
if(tmp<=M) ans=max(ans,R-L+);
}
}
rep(i,,N){
int L=,R=,tmp=;
while(i+R<=N&&i--R>=){
R++; tmp+=abs(c[i-+R]-c[i-R]);
while(tmp>M) tmp-=abs(c[i-+L]-c[i-L]),L++;
if(tmp<=M) ans=max(ans,R-L+);
}
}
}
int main()
{
scanf("%d",&T);
while(T--){
scanf("%d%s",&M,c+); N=strlen(c+);
ans=; solve();
printf("%d\n",ans);
}
return ;
}

HDU - 6103 :Kirinriki(不错的尺取法)的更多相关文章

  1. 2017ACM暑期多校联合训练 - Team 6 1008 HDU 6103 Kirinriki (模拟 尺取法)

    题目链接 Problem Description We define the distance of two strings A and B with same length n is disA,B= ...

  2. HDU 6103 Kirinriki(尺取法)

    http://acm.hdu.edu.cn/showproblem.php?pid=6103 题意: 给出一个字符串,在其中找两串互不重叠的子串,计算它们之间的dis值,要求dis值小于等于m,求能选 ...

  3. HDU 5358 First One 数学+尺取法

    多校的题,摆明了数学题,但是没想出来,蠢爆了,之前算了半天的s[i][j]的和,其实是积.其实比赛的时候我连log(s[i][j])+1是s[i][j]的位数都没看出来,说出来都丢人. 知道了这个之后 ...

  4. hdu 6205 card card card 尺取法

    card card card Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)To ...

  5. HDU 6103 Kirinriki (思维 双指针)

    Kirinriki Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  6. hdu 6103(Kirinriki)

    题目链接:Kirinriki 题目描述: 找两个不重叠的字符串A,B. 使得dis(A,B)<=m;\(dis(A,B)= \sum _{i=0}^{n-1} \left | A_i-B_{n- ...

  7. Face The Right Way 一道不错的尺取法和标记法题目。 poj 3276

    Face The Right Way Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 2899   Accepted: 133 ...

  8. hdu 4737 A Bit Fun 尺取法

    A Bit Fun Time Limit: 5000/2500 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Proble ...

  9. hdu 6103 Kirinriki (枚举对称中心+双指针)

    Problem Description We define the distance of two strings A and B with same length n isdisA,B=∑(i=0 ...

随机推荐

  1. NEVER QUIT. NEVER SAY NEVER.

    有志者不是从不失败,而是从不妥协. NEVER QUIT. NEVER SAY NEVER. 2015/09/15 Winners are not those who never fail but t ...

  2. MySQL数据库表分区功能详解

    1.什么是表分区? mysql数据库中的数据是以文件的形势存在磁盘上的,默认放在/mysql/data下面(可以通过my.cnf中的datadir来查看),一张表主要对应着三个文件,一个是frm存放表 ...

  3. 20162326 齐力锋 2017-2018学期 Bag类的补写博客

    要求: 代码运行在命令行中,路径要体现学号信息,IDEA中,伪代码要体现个人学号信息 参见Bag的UML图,用Java继承BagInterface实现泛型类Bag,并对方法进行单元测试(JUnit), ...

  4. Appium+eclipse+python环境配置

    1.安装安卓开发环境(教程很多,不细写) 2.安装eclipse 下载eclipse,解压即可   3.安装python 下载地址:https://www.python.org/downloads/r ...

  5. React Native之持久化存储(AsyncStorage、react-native-storage)的使用

    AsyncStorage是一个简单的.异步的.持久化的Key-Value存储系统,它对于App来说是全局性的.这是官网上对它的介绍.可以知道,这个asyncstorage也是以键值对的形式进行存储数据 ...

  6. Linux图形化界面下使用命令进行截图的方法

    以前在LINUX里面截图都是直接按print screen键或者 alt + print screen. 但是print screen是整个屏幕, alt + print screen是当前窗口. 想 ...

  7. SpringBoot ControllerAdvice

    在Spring3.2中新增了@ControllerAdvice注解,可用于定义@ExceptionHandler @ModelAttribute @InitBinder,并应用到所有被@Request ...

  8. Python 面向对象的三大特性:封装,继承,多态

    # 面向对象的三大特性:封装,继承,多态 # 继承的影响:资源的继承,资源的使用,资源的覆盖,资源的累加 # 资源的继承,在Python中的继承是指能使用父类的资源,而不是说在子类也复制一份父类代码到 ...

  9. Java基本数据类型与相应的封装类

    基本数据类型   封装类 int Integer short   Short float   Float double   Double long   Long boolean   Boolean b ...

  10. C语言排序算法之简单交换法排序,直接选择排序,冒泡排序

    C语言排序算法之简单交换法排序,直接选择排序,冒泡排序,最近考试要用到,网上也有很多例子,我觉得还是自己写的看得懂一些. 简单交换法排序 /*简单交换法排序 根据序列中两个记录键值的比较结果来对换这两 ...