HDU 6103 17多校6 Kirinriki(双指针维护)
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.
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
[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
题目描述:
找两个不重叠的字符串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(双指针维护)的更多相关文章
- HDU 6140 17多校8 Hybrid Crystals(思维题)
题目传送: Hybrid Crystals Problem Description > Kyber crystals, also called the living crystal or sim ...
- HDU 6143 17多校8 Killer Names(组合数学)
题目传送:Killer Names Problem Description > Galen Marek, codenamed Starkiller, was a male Human appre ...
- HDU 6045 17多校2 Is Derek lying?
题目传送:http://acm.hdu.edu.cn/showproblem.php?pid=6045 Time Limit: 3000/1000 MS (Java/Others) Memory ...
- HDU 6124 17多校7 Euler theorem(简单思维题)
Problem Description HazelFan is given two positive integers a,b, and he wants to calculate amodb. Bu ...
- 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 ...
- 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 ...
- HDU 6034 17多校1 Balala Power!(思维 排序)
Problem Description Talented Mr.Tang has n strings consisting of only lower case characters. He want ...
- 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. ...
- HDU 6106 17多校6 Classes(容斥简单题)
Problem Description The school set up three elective courses, assuming that these courses are A, B, ...
随机推荐
- python--model进阶
一.QuerySet 1.可切片 使用Python 的切片语法来限制查询集记录的数目 .它等同于SQL 的LIMIT 和OFFSET 子句. >>> Entry.objects.a ...
- 转-如何使用iTunes制作iPhone铃声
新版iTunes(iTunes11)推出以后,界面上发生了一些改变,给人带来一种面貌一新的感觉,但也给许多朋友带来一些操作上的不太适应.下面就大家比较关心的iPhone的铃声制作方法,我在iTunes ...
- maven聚合工程tomcat插件启动没有 Starting ProtocolHandler ["http-bio-8081"]
Starting ProtocolHandler ["http-bio-8081"]无法显示,一般有三个原因: (1)数据库连不上: (2)注册中心连不上(我这里用的是zookee ...
- mvn 修改所有子项目pom版本
mvn versions:set -DgenerateBackupPoms=false -DnewVersion=1.3.0
- python3练习-装饰器
在廖雪峰的官方网站学习装饰器章节时,初步理解类似与面向切面编程.记录一下自己的课后习题解法. 问题: 请编写一个decorator,能在函数调用的前后打印出'begin call'和'end call ...
- kiss word memory post poly peri out ~p 4
1● post p əust 在后面,邮件 2● peri 多 3● poly 周围,靠近
- std::string find 的返回值
std::string 的方法 find,返回值类型是std::string::size_type, 对应的是查找对象在字符串中的位置(从0开始), 如果未查找到,该返回值是一个很大的数据(4294 ...
- laravel中的plicy授权方法:
1.用命令新建policy: php artisan make:policy PostPolicy 2.在app/Policies/PostPolicy.php中添加处理文件的权限的方法: //修改: ...
- laravel创建新的提交数据
public function store() { $this->validate(request(),[ 'title'=>'required|string|max:100|min:10 ...
- ไม่มีวันเช่น--会有那么一天--电影《初恋这件小事》插曲--IPA--泰语
小清新的泰国的<初恋这件小事>插曲.