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. 什么是JIT?

    JIT是just in time,即时编译技术.使用该技术,能够加速java程序的执行速度.下面,就对该技术做个简单的讲解. 首先,我们大家都知道,通常javac将程序源代码编译,转换成java字节码 ...

  2. 剑指offer编程题66道题 1-25

    1.二维数组中的查找 题目描述 在一个二维数组中,每一行都按照从左到右递增的顺序排序,每一列都按照从上到下递增的顺序排序.请完成一个函数,输入这样的一个二维数组和一个整数,判断数组中是否含有该整数. ...

  3. Javascript作用域详解。

    javascript的作用域 是按照   函数来划分的. 网址:http://www.cnblogs.com/rubylouvre/archive/2009/08/21/1551270.html

  4. 关于git bash的问题,pull不下来(登录之后,git帮你记住了,想切换其他用户)

    参考博客: https://www.jianshu.com/p/8a7f257e07b8 从某个项目地址pull代码下来,老是报错 fatal: Authentication failed for ' ...

  5. setState详解

    我们都知道,React通过this.state来访问state,通过this.setState()方法来更新state.当this.setState()方法被调用的时候,React会重新调用rende ...

  6. java基础(5)--流程控制结构

    流程控制结构 if结构 当关系表达式为true时,执行语句 if(关系表达式){ //语句块 } if-else结构 当关系表达式为true时,执行语句块1,否则执行语句块2 if(关系表达式){ / ...

  7. Multiply Strings,字符串相乘

    问题描述:给定两个字符串,返回他们的乘积. public class MultiplyStrings { public String multiply(String num1, String num2 ...

  8. 指定library路径

    1.执行 ?.jar文件: 1.1.“java -jar ?.jar” 1.2.如果 ?.jar里面使用了JNI调用了 ?.dll/?.so 等文件,可能会报错 找不到相关的 库文件,如果这样的话,可 ...

  9. CSS打造固定表头

    html代码: <!DOCTYPE html> <html> <head lang="en"> <meta charset="U ...

  10. 22个HTML5的初级技巧

    Web技术的发展速度太快了,如果你不与时俱进,就会被淘汰.因此,为了应对即将到来的HTML5,本文总结了22个HTML5的初级技巧,希望能对你进一步学习好HTML5会有所帮助. 1. 新的Doctyp ...