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. flask 根路由在蓝图中

  2. Optional常用操作

    1. 常见操作 @Test public void test1() { F f = new F(); // of(非null对象) Optional<F> fOptional = Opti ...

  3. Android Studio使用tips

    安装位置:C:\Users\xxx\AppData\Local\Android\sdk https://developer.android.com/topic/libraries/support-li ...

  4. [CSP-S模拟测试]:赤壁情(DP)

    前赤壁赋 壬戌之秋,七月既望,苏子与客泛舟游于赤壁之下.清风徐来,水波不兴.举酒属客,诵明月之诗,歌窈窕之章.少焉,月出于东山之上,徘徊于斗牛之间.白露横江,水光接天.纵一苇之所如,凌万顷之茫然.浩浩 ...

  5. LINUX 文件合并,去重

    (1)两个文件的交集,并集前提条件:每个文件中不得有重复行1. 取出两个文件的并集(重复的行只保留一份)cat file1 file2 | sort | uniq > file32. 取出两个文 ...

  6. Python 进阶_OOP 面向对象编程_self 的实例绑定

    目录 目录 self 和绑定 调用非绑定的方法 self 和绑定 在 Python 中 self 变量是特殊的, 其用于在实例方法中引用该方法所绑定的实例, 换句话说就是 Python 在实例化对象时 ...

  7. Vue&webpack入门实践

    目录 1. 下载安装Vue 2. Vue 2.1 Vue要素 2.2 指令 2.3 组件化 2.4 vue-router 3. webpack 3.1 webpack简介 3.2 四个核心概念 3.3 ...

  8. Javascript在ajax提交过程中页面显示加载中,请等待效果,并在提交过程中限制确定按钮防止多次提交,提交完成后,解除提交限制

    加载中,请等待div: <div id="load" class="center-in-center" style="display:none; ...

  9. git 裸库

    初始化一个空的裸仓库 $ cd /home/repo $ mkdir tproject.git $ cd tproject.git $ git init - -bare      注:这是在服务器上运 ...

  10. js-xlsx sheet_to_json 读取小数位数变多

    read as string . 例如:2.85 读取后变成 2.84999999999999999 这种. 以字符串形式读取. XLSX.utils.sheet_to_json(workbook.S ...