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. 【IDEA】【3】操作使用

    前言: 1,显示工具栏及底部周边工具栏 2,修改项目名称 3,文件重命名 4,鼠标移动到方法上时进行提示注释 5,修改文件后自动重启 6,查看代码覆盖率 正文: 1,显示工具栏及底部周边工具栏 Vie ...

  2. seaweedFS

    那首先我们来分析一下seaweedfs是什么?seaweedfs是一个非常优秀的由 golang 开发的分布式存储开源项目.它是用来存储文件的系统,并且与使用的语言无关,使得文件储存在云端变得非常方便 ...

  3. 1002. Find Common Characters查找常用字符

    参考:https://leetcode.com/problems/find-common-characters/discuss/247573/C%2B%2B-O(n)-or-O(1)-two-vect ...

  4. 基本数据类型dict

    1. 字典 dict 用{}来表示 键值对数据 {key:value} 唯一性 键 都必须是可哈希的 不可变的数据类型就可以当做字典中的键 可哈希不可变的数据类型:int str tuple bool ...

  5. 基本数据类型list,tuple

    一. 列表 1. 列表是一个可变的数据类型 列表由[]来表示, 每一项元素使用逗号隔开. 列表什么都能装. 能装对象的对象. 列表可以装大量的数据 列表是有序的 2. 列表的索引和切片 列表和字符串一 ...

  6. PinkEx专用图标

  7. 建一个maven项目

     建一个普通的maven项目(eclipse) 需要的jar和文件: eclipse :jdk1.8.0_144 maven:apache-maven-3.5.3     进入(下载):http:// ...

  8. Boost.PropertyTree读取ini文件(Linux环境)

    昨天因为需要读取配置文件略略伤神.网上很多例子但是我用都会报错,很多人把Boost.PropertyTree的函数写很麻烦的包所以报错我也不知道什么问题,所以今天整理下. 头上附上官网链接:Boost ...

  9. linux网络操作 配置文件

    网络接口配置文件(网卡信息文件) '/etc/sysconfig/network-srcipts/ifcfg-*(eth0)' (注意区分大小写) DEVICE=eth0 网卡编号 HWADDR=08 ...

  10. Uva 10635 - Prince and Princess 问题转化,元素互不相同(在自身序列中独特)的两个数列的LCS,LIS 难度: 2

    题目 https://uva.onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&page=show_problem&a ...