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

 
 
启发博客:http://www.cnblogs.com/coded-ream/p/7343946.html
以下题解和题意摘自此博客

题目描述:

找两个不重叠的字符串A,B。 使得dis(A,B)<=m;dis(A,B)=∑i=0n−1|Ai−Bn−1−i|。求最长的字符串长度。

思路:

官方题解,双指针维护。简单题。枚举对称中心。

 双指针维护!!以后要搜超时就记这个!!
 #include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
#include<map>
#include<vector>
#include<cmath>
#include<cstring>
using namespace std;
int m,ans,len;
char str[]; void solve(int x,int y)//x,y表示左右端点
{
int dis=;
int l=,r=;//双指针,r记录的是两段各自的长度,l记录的是头尾各自缩进多少
while(x+r<y-r)
{
if(dis+abs(str[x+r]-str[y-r])<=m)
{
dis+=abs(str[x+r]-str[y-r]);
r++;
ans=max(ans,r-l);
}
else
{
dis-=abs(str[x+l]-str[y-l]);
l++;
}
}
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
scanf("%d",&m);
cin>>str;
ans=;
len=strlen(str);
for(int i=;i<len;i++)
solve(,i);//相当于枚举对称轴在前半段
for(int i=;i<len-;i++)
solve(i,len-);//相当于枚举对称轴在后半段
printf("%d\n",ans);
}
return ;
}

HDU 6103 17多校6 Kirinriki(双指针维护)的更多相关文章

  1. HDU 6140 17多校8 Hybrid Crystals(思维题)

    题目传送: Hybrid Crystals Problem Description > Kyber crystals, also called the living crystal or sim ...

  2. HDU 6143 17多校8 Killer Names(组合数学)

    题目传送:Killer Names Problem Description > Galen Marek, codenamed Starkiller, was a male Human appre ...

  3. HDU 6045 17多校2 Is Derek lying?

    题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=6045 Time Limit: 3000/1000 MS (Java/Others)    Memory ...

  4. HDU 6124 17多校7 Euler theorem(简单思维题)

    Problem Description HazelFan is given two positive integers a,b, and he wants to calculate amodb. Bu ...

  5. HDU 3130 17多校7 Kolakoski(思维简单)

    Problem Description This is Kolakosiki sequence: 1,2,2,1,1,2,1,2,2,1,2,2,1,1,2,1,1,2,2,1……. This seq ...

  6. HDU 6038 17多校1 Function(找循环节/环)

    Problem Description You are given a permutation a from 0 to n−1 and a permutation b from 0 to m−1. D ...

  7. HDU 6034 17多校1 Balala Power!(思维 排序)

    Problem Description Talented Mr.Tang has n strings consisting of only lower case characters. He want ...

  8. HDU 6098 17多校6 Inversion(思维+优化)

    Problem Description Give an array A, the index starts from 1.Now we want to know Bi=maxi∤jAj , i≥2. ...

  9. HDU 6106 17多校6 Classes(容斥简单题)

    Problem Description The school set up three elective courses, assuming that these courses are A, B, ...

随机推荐

  1. linux指令统计日志出现的次数

    cat  XXX.log|grep ''|grep '条件'| wc -l   单个条件统计 cat  XXX.log|grep ''|grep '条件1'|grep '条件2'|grep '条件3' ...

  2. GSON使用之对特殊字符的转换的处理

    很多人是在转换时特殊字符被替换成了unicode编程格式,而我碰到的类似,只不过是后台转换成json字符串到前端,前端解析时 '' 双引号和 / 斜杠被原样转换,冲突了json的关键字符,导致解析时提 ...

  3. oracle 常用字符串函数

    select  initcap('guodongdong') from dual;                                  /返回字符串并将字符串的第一个字母变为大写;  s ...

  4. maven聚合工程tomcat插件启动没有 Starting ProtocolHandler ["http-bio-8081"]

    Starting ProtocolHandler ["http-bio-8081"]无法显示,一般有三个原因: (1)数据库连不上: (2)注册中心连不上(我这里用的是zookee ...

  5. 《高性能SQL调优精要与案例解析》一书谈SQL调优(SQL TUNING或SQL优化)学习

    <高性能SQL调优精要与案例解析>一书上市发售以来,很多热心读者就该书内容及一些具体问题提出了疑问,因读者众多外加本人日常工作的繁忙 ,在这里就SQL调优学习进行讨论并对热点问题统一作答. ...

  6. eclipse安装scala环境

    1.安装eclipse插件,依次点击Help->Eclipse Marketplace 2.输入scala,点击go,进行搜索 3,出现了Scala IDE4.7X,点击右下方的Install进 ...

  7. [Java] 各种流的分类及区别

    https://www.cnblogs.com/lca1826/p/6427177.html 流在Java中是指计算中流动的缓冲区. 从外部设备流向中央处理器的数据流成为“输入流”,反之成为“输出流” ...

  8. java 数据溢出和编译错误的差别

    int a=100000000000;编译错误,超出int范围 int a=2100000000; int b=a*12020200;数据溢出,a并未溢出,但b在通过a计算后的数据溢出 long e= ...

  9. js-数组方法的使用和详谈

    写博客的同时也是对自己知识的一次全面总结,方便自己日后复习.今天总结一下JS中Array的所有方法和技巧,对算法题算是一个基础了,有不足的地方,还望童鞋们指出来,一起进步. 在总结方法之前,提到一点, ...

  10. 每天CSS学习之transform-origin

    在上一篇中,我们学习了如何使用transform来进行2D变形.今天要讲述的transform-origin与这个变形有关. origin翻译过来的意思是原点.开端.transform-origin寓 ...