Problem Description
We define the distance of two strings A and B with same length n is
disA,B=∑(i=0  n−1)  |A[i]−B[n−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

 
给你一个m值,一个字符串,让你在字符串中找两个不重叠的长度相等的字串,两个字符串首对应尾逐项向中心的acsii码差的绝对值之和不能大于m
问这两个相等的字符串最长有多长
 
我们枚举这两个字串的对称中心,对于每个对称中心我们利用双指针进行处理一下就好了
代码如下:
 #include <bits/stdc++.h>

 using namespace std;
const int maxn = +;
char s[maxn];
int m,len;
int ans;
void work (int x,int y)
{
int dis=,l=,r=;//左边的串s[x-l]~s[x-r] 右边的串s[y+l]~s[y+r]
while (y+r<len&&x-r>=){
if (dis+abs(s[x-r]-s[y+r])<=m){
dis+=abs(s[x-r]-s[y+r]);
r++;
ans = max(ans,r-l);
}
else{
dis-=abs(s[x-l]-s[y+l]);
l++;
}
}
}
int main()
{
//freopen("de.txt","r",stdin);
int T;
scanf("%d",&T);
while (T--){
scanf("%d",&m);
scanf("%s",s);
len = strlen(s);
ans = ;
for (int i=;i<len;++i){
work(i-,i+);
work(i,i+);
}
printf("%d\n",ans);
}
return ;
}
 

hdu 6103 Kirinriki (枚举对称中心+双指针)的更多相关文章

  1. HDU 6103 Kirinriki (思维 双指针)

    Kirinriki Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total S ...

  2. hdu 6103(Kirinriki)

    题目链接:Kirinriki 题目描述: 找两个不重叠的字符串A,B. 使得dis(A,B)<=m;\(dis(A,B)= \sum _{i=0}^{n-1} \left | A_i-B_{n- ...

  3. HDU 6103 Kirinriki(尺取法)

    http://acm.hdu.edu.cn/showproblem.php?pid=6103 题意: 给出一个字符串,在其中找两串互不重叠的子串,计算它们之间的dis值,要求dis值小于等于m,求能选 ...

  4. 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= ...

  5. HDU - 6103 :Kirinriki(不错的尺取法)

    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 ...

  6. HDU 6103 17多校6 Kirinriki(双指针维护)

    Problem Description We define the distance of two strings A and B with same length n isdisA,B=∑i=0n− ...

  7. Hdu 5806 NanoApe Loves Sequence Ⅱ(双指针) (C++,Java)

    Hdu 5806 NanoApe Loves Sequence Ⅱ(双指针) Hdu 5806 题意:给出一个数组,求区间第k大的数大于等于m的区间个数 #include<queue> # ...

  8. 2015多校第6场 HDU 5358 First One 枚举,双指针

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5358 题意:如题. 解法:观察式子发现,由于log函数的存在,使得这个函数的值域<=34,然后我 ...

  9. hdu 2489(枚举 + 最小生成树)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2489 思路:由于N, M的范围比较少,直接枚举所有的可能情况,然后求MST判断即可. #include ...

随机推荐

  1. POJ 3481 Double Queue (treap模板)

    Description The new founded Balkan Investment Group Bank (BIG-Bank) opened a new office in Bucharest ...

  2. 【LeetCode 60】第k个排列

    题目链接 [题解] 逆康托展开. 考虑康托展开的过程. K = ∑v[i]*(n-i)! 其中v[i]表示在a[i+1..n]中比a[i]小的数字的个数 (也即未出现的数字中它排名第几(从0开始)) ...

  3. Django-template模板语言

    一.常用语法 只需要记两种特殊符号: {{  }}和 {% %} 变量相关的用{{}},逻辑相关的用{%%}. 首先把views里代码贴出了,之后就是在HTML中各种模板语言替换了 本质都是字符串的替 ...

  4. winserver安装redis

    原文: https://www.cnblogs.com/xuzhiwei/p/4569315.html 1.Redis本身不支持windows,但是有另外的团队在维护着一个windows下的版本   ...

  5. 4 November in ss

    Contest A. 输油管道问题 某石油公司计划建造一条由东向西的主输油管道.该管道要穿过一个有 \(n\) 口油井的油田.从每口油井都要有一条输油管道沿最短路经 (或南或北) 与主管道相连.如果给 ...

  6. mysql查看sql执行情况的几种方法

    mysql系统变量分为全局变量和会话变量,全局变量的修改影响到整个服务器,会话变量修改只影响当前的会话. 查看log日志是否开启 show variables like 'general_log' s ...

  7. GH001 on github

    remote: warning: GH001: Large files detected. You may want to try Git Large File Storage - https://g ...

  8. linux主机hang住echo 0 > /proc/sys/kernel/hung_task_timeout_secs disables this message

    用dmesg显示如下图信息 问题原因: 默认情况下, Linux会最多使用40%的可用内存作为文件系统缓存.当超过这个阈值后,文件系统会把将缓存中的内存全部写入磁盘, 导致后续的IO请求都是同步的. ...

  9. nginx配置相关问题

    1. nginx配置ssl相关问题 1.1 报错nginx: [emerg] the "ssl" parameter requires ngx_http_ssl_module in ...

  10. MSF——Payload模块(二)

    MSF系列: MSF——基本使用和Exploit模块(一) MSF——Payload模块(二) MSF——Meterpreter(三) MSF——信息收集(四) 一.exploit和payload e ...