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. Nginx 限制php解析、限制浏览器访问

    限制php解析 1.有时候会根据目录来限制php解析: location ~ .*(diy|template|attachments|forumdata|attachment|image)/.*\.p ...

  2. openstack认证实践

    环境: 客户端:负责发送请求, 服务器:负责接受请求: 认证服务器keystone:负责认证 具体认证步骤: 1.客户端首先进行签名计算,将得到的签名字符串作为authorization发给keyst ...

  3. curator的版本兼容问题(需注意)

    Curator 存在版本兼容问题. Curator 2.x.x-兼容两个zk 3.4.x 和zk 3.5.x, Curator 3.x.x-兼容兼容zk 3.5. Versions The are c ...

  4. 主机不能访问虚拟机web服务的问题

    虚拟机是CentOs 7 iptables -Fiptables -P INPUT ACCEPT 参考:http://blog.csdn.net/abnereel/article/details/41 ...

  5. Spring Boot 热部署的实现 - 原创

    实现方式有两大种(其中包含3种): 一.基于springloaded 1.1)Maven启动方式 第一步:在pom.xml中的“plugin节点”里面添加如下依赖: <dependencies& ...

  6. LongestValidParentheses, 求最长合法括号子串长度-----同类问题ValidParentheses,GenerateParentheses

    问题描述:求括号字符串中最长合法子串长度.例如:()((),返回2,而不是4. 算法分析:还是利用栈,和判断合法括号对是一样的. public static int longestValidParen ...

  7. spring3: 表达式5.2 SpEL基础

    5.1  概述 5.1.1  概述 Spring表达式语言全称为“Spring Expression Language”,缩写为“SpEL”,类似于Struts2x中使用的OGNL表达式语言,能在运行 ...

  8. 调用http接口的工具类

    网上面有很多,但是我们项目怎么也调不到结果,试了差不多很多案例,都是报connection reset 后来,我发现是有一个验证,需要跳过验证.然后才能调接口.所以找了一个忽略https的方法.进行改 ...

  9. poj2478欧拉函数

    打表欧拉函数,求2到n的欧拉函数和 #include<map> #include<set> #include<cmath> #include<queue> ...

  10. Oracle11g数据库监听配置

    (转自:http://blog.sina.com.cn/s/blog_6908928501018057.html) 经验告诉我:最好把数据库的SID和数据库全局名称分开,免得配置时混了,如果要配置服务 ...