HDU - 6103 :Kirinriki(不错的尺取法)
dis A,B =∑ i=0 n−1 |A i −B n−1−i | 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.
InputThe 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 T≤100
0≤m≤5000 0≤m≤5000
Each character in the string is lowercase letter, 2≤|S|≤5000 2≤|S|≤5000
∑|S|≤20000 ∑|S|≤20000
OutputFor 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
题意:给堵一个字符串,求最长的两个不相交字串S、T,其字符串值之差(倒序的字符之差的绝对值之和)小于M,输出这个长度。
思路:尺取法,枚举起点终点发现没法做,我们枚举S和T的对称点,然后根据对称点尺取。即每次右边界++,维护左边界,使其满足小于M。
主要是利用了此题中,字符串之差是首尾倒序做差,我们我们可以这样处理。 有点像求回文串一样。
#include<bits/stdc++.h>
#define rep(i,a,b) for(int i=a;i<=b;i++)
using namespace std;
char c[]; int T,N,M,ans;
void solve()
{
rep(i,,N){
int L=,R=,tmp=;
while(i+R+<=N&&i-R->=){
R++; tmp+=abs(c[i+R]-c[i-R]);
while(tmp>M) tmp-=abs(c[i+L]-c[i-L]),L++;
if(tmp<=M) ans=max(ans,R-L+);
}
}
rep(i,,N){
int L=,R=,tmp=;
while(i+R<=N&&i--R>=){
R++; tmp+=abs(c[i-+R]-c[i-R]);
while(tmp>M) tmp-=abs(c[i-+L]-c[i-L]),L++;
if(tmp<=M) ans=max(ans,R-L+);
}
}
}
int main()
{
scanf("%d",&T);
while(T--){
scanf("%d%s",&M,c+); N=strlen(c+);
ans=; solve();
printf("%d\n",ans);
}
return ;
}
HDU - 6103 :Kirinriki(不错的尺取法)的更多相关文章
- 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= ...
- HDU 6103 Kirinriki(尺取法)
http://acm.hdu.edu.cn/showproblem.php?pid=6103 题意: 给出一个字符串,在其中找两串互不重叠的子串,计算它们之间的dis值,要求dis值小于等于m,求能选 ...
- HDU 5358 First One 数学+尺取法
多校的题,摆明了数学题,但是没想出来,蠢爆了,之前算了半天的s[i][j]的和,其实是积.其实比赛的时候我连log(s[i][j])+1是s[i][j]的位数都没看出来,说出来都丢人. 知道了这个之后 ...
- hdu 6205 card card card 尺取法
card card card Time Limit: 8000/4000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)To ...
- HDU 6103 Kirinriki (思维 双指针)
Kirinriki Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- hdu 6103(Kirinriki)
题目链接:Kirinriki 题目描述: 找两个不重叠的字符串A,B. 使得dis(A,B)<=m;\(dis(A,B)= \sum _{i=0}^{n-1} \left | A_i-B_{n- ...
- Face The Right Way 一道不错的尺取法和标记法题目。 poj 3276
Face The Right Way Time Limit: 2000MS Memory Limit: 65536K Total Submissions: 2899 Accepted: 133 ...
- hdu 4737 A Bit Fun 尺取法
A Bit Fun Time Limit: 5000/2500 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Proble ...
- hdu 6103 Kirinriki (枚举对称中心+双指针)
Problem Description We define the distance of two strings A and B with same length n isdisA,B=∑(i=0 ...
随机推荐
- Jconsle
1. jconsole 远程连接: JConsole很好用,可以解决很多疑难杂症.但远程连接需要设置一下Java opt才可以使用.以下是步骤: 1). 在java opt下添加如下内容: 如果是无须 ...
- js获取iframe和父级之间元素,方法、属,获取iframe的高度自适应iframe高度
摘自:http://blog.csdn.net/kongjiea/article/details/38870399 1.在父页面 获取iframe子页面的元素 (在同域的情况下 且在http://下测 ...
- linux驱动分层分离思想
转:https://blog.csdn.net/zqixiao_09/article/details/51088887 前面我们学习I2C.USB.SD驱动时,有没有发现一个共性,就是在驱动开发时,每 ...
- jQuery垂直手风琴菜单 菜单项带小图标
在线演示 本地下载
- InterruptedException异常
本文总结自:https://blog.csdn.net/asdfsadfasdfsa/article/details/78808131 什么样的方法会抛出InterruptedException异常? ...
- Linux下使用USB模拟ACM串口设备【转】
本文转载自:https://www.cnblogs.com/pied/p/4549614.html 这个想法之前就在脑袋里有过,最近公司产品要用到,所以多做了些了解. 1. USB 简介 USB 是 ...
- JAVA中的数据存储(堆及堆栈)- 转载
1.寄存器:最快的存储区, 由编译器根据需求进行分配,我们在程序中无法控制.2. 栈:存放基本类型的变量数据和对象的引用,但对象本身不存放在栈中,而是存放在堆(new 出来的对象)或者常量池中(对象可 ...
- scala学习手记14 - 单例对象
java中的单例模式都很熟悉了:简单地说就是一个类只能有一个实例.在scala中创建单例对象非常简单,创建类时使用object关键字替换class即可.因为单例类无法初始化,所以不能向它的主构造函数传 ...
- elsevier 与 springer 投稿区别
http://emuch.net/bbs/viewthread.php?tid=5369913
- ng2 学习笔记(三)依赖注入与服务
前两篇文章简单介绍了ng2的一些基础用法,基本和ng1的使用风格差不多,只是写法和开发方式变化比较大. 这一篇,来总结一下ng的依赖注入与服务.官方的教程上是把他分开来讲的,个人感觉放在一起比较容易理 ...