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. Educational Codeforces Round 55 (Rated for Div. 2) A/B/C/D

    http://codeforces.com/contest/1082/problem/A WA数发,因为默认为x<y = = 分情况讨论,直达 or x->1->y  or  x-& ...

  2. Qt之获取子部件

    Qt5.10 QList<QObject*> list_children = this->children(); for(int i=0;i<list_children.siz ...

  3. Mockito:一个强大的用于Java开发的模拟测试框架

    https://blog.csdn.net/zhoudaxia/article/details/33056093 介绍 本文将介绍模拟测试框架Mockito的一些基础概念, 介绍该框架的优点,讲解应用 ...

  4. linux ssh root登陆出现错误:Permission denied, please try again

    密码已检测过多遍还是登录失败 经检查 vim /etc/ssh/sshd_config PermitRootLogin no 改成 PermitRootLogin yes 修改之后重启就可以了

  5. POJ 2456 Agressive cows(二分)

    POJ 2456 Agressive cows 农夫 John 建造了一座很长的畜栏,它包括N (2≤N≤100,000)个隔间,这 些小隔间的位置为x0,...,xN-1 (0≤xi≤1,000,0 ...

  6. linux系统管理 设置别名

    查看和设定别名 'alias' 查看系统中所有的命令别名 [root@ssgao1987 ~]# alias alias cp='cp -i' alias l.='ls -d .* --color=a ...

  7. inode占用100%时硬盘无法写入文件故障处理

    故障现象: 分区无法写入文件. 故障分析: 执行df -h命令发现空间占用不到50%,执行df -hi,发现某分区IUse%值为99%,说明innode已经用完,应该是某些目录下存在大量的小文件导致. ...

  8. alpine linux docker 安装 lxml出错的解决办法。

    我习惯在docker当中用alpine来部署服务. 最近在部署flask时使用了 tiangolo/uwsgi-nginx-flask:python3.6-alpine3.7 这个镜像 别人写好的fl ...

  9. 生产者与消费者问题,C++利用bind基于对象实现与面向对象实现

    body, table{font-family: 微软雅黑; font-size: 10pt} table{border-collapse: collapse; border: solid gray; ...

  10. 关于iOS构建版本

    1.Build Active Architecture Only 设置 Build Active Architecture Only 设置为NO的时候,会编译支持的所有的版本 设置为YES的时候,是为 ...