题目链接

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

题意:

给定一个字符串与一个距离dis的最大限制,在该串中找到两个等长的无交叉的子串,求在满足dis限制下的最长的子串的长度。

dis的定义为:disA,B=∑i=0n−1|Ai−Bn−1−i|,A、B分别从给定字符串中截取的等长度的无交叉的子串。

分析:

最原来以为要一个一个的循环遍历,这样的话时间复杂度太大,中间也有很多的重复的部分,考虑到这些因素的话,用尺取法来考虑这个问题。

首先我们可以假设 A 一定在 B 左边,然后我们可以考虑A的起点和B的尾部,如果暂时不考虑长度不固定,我们每次查找都让长度尽可能长,那么,我们一定需要将 A的起点和B的尾部,然后获取 A 向右延伸,B 向左延伸对应位置的贡献,然后呢?我们可以采用尺取法来进行A的起点和B的尾部向中间的推移,当 此时的dis值大于 m 时,我们向中间推移 A的起点和B的尾部即可。在这个尺取法的过程中,记录下来最大的 dis 值即可。

简单的说就是,将整个区间划分为两份,然后左右各有一个序列,这个序列是贪心的,贪心的过程利用尺取法来维护最优解,而这里我们需要对区间的划分进行枚举。

这样的话考虑到一种情况就是每次的时候,我们都是在用这个字符串的后半部分来匹配前半部分的任意一种情况,相当于后半部分的字符串是基本上固定的,这样的话匹配的可能行机会减少,为了解决设个问题,我们只需要将字符串逆序,再按照你这的字符串同样的方法比较一下,找出其中的最大值即可。

代码:

#include<bits/stdc++.h>
using namespace std; const int maxn=21000;
char str[maxn];
int m,len; int solve()
{
int ans=0;
for(int i=len; i>=1; i--)///B串的结尾处
{
int cnt=i/2-1,d=0,t=0,p=0;///cnt表示的是A串的结尾处,d表示的是当前串的dis,t表示的是串的长度
for(int j=0; j<=cnt; j++)///j表示的是A串的开始坐标
{
d+=abs(str[1+j]-str[i-j]);///串的长度往后扩展
if(d>m)///这里表示的是当前串的dis超过限制,将串从前面减少,然后从后面增加
{
d-=abs(str[1+p]-str[i-p]);
d-=abs(str[1+j]-str[i-j]);
p++;///前面减去的串的长度
t--;///串的长度也要变化
j--;
}
else
{
t++;
ans=max(ans,t);
}
}
}
return ans;
} int main()
{
int T;
scanf("%d",&T);
while(T--)
{
int ans=0;
scanf("%d%s",&m,str+1);///从第一位开始赋值
len=strlen(str+1);
ans=max(ans,solve());///正序的这次是拿后面的那一部分来与前面的那一部分进行匹配
reverse(str+1,str+len+1);
ans=max(ans,solve());///反序的正好反过来,是用原序列里面前面的那一部分来匹配后面的那一部分
printf("%d\n",ans);
}
return 0;
}

2017ACM暑期多校联合训练 - Team 6 1008 HDU 6103 Kirinriki (模拟 尺取法)的更多相关文章

  1. 2017ACM暑期多校联合训练 - Team 8 1008 HDU 6140 Hybrid Crystals (模拟)

    题目链接 Problem Description Kyber crystals, also called the living crystal or simply the kyber, and kno ...

  2. 2017ACM暑期多校联合训练 - Team 7 1008 HDU 6127 Hard challenge (极角排序)

    题目链接 Problem Description There are n points on the plane, and the ith points has a value vali, and i ...

  3. 2017ACM暑期多校联合训练 - Team 2 1008 HDU 6052 To my boyfriend (数学 模拟)

    题目链接 Problem Description Dear Liao I never forget the moment I met with you. You carefully asked me: ...

  4. 2017ACM暑期多校联合训练 - Team 4 1004 HDU 6070 Dirt Ratio (线段树)

    题目链接 Problem Description In ACM/ICPC contest, the ''Dirt Ratio'' of a team is calculated in the foll ...

  5. 2017ACM暑期多校联合训练 - Team 9 1005 HDU 6165 FFF at Valentine (dfs)

    题目链接 Problem Description At Valentine's eve, Shylock and Lucar were enjoying their time as any other ...

  6. 2017ACM暑期多校联合训练 - Team 9 1010 HDU 6170 Two strings (dp)

    题目链接 Problem Description Giving two strings and you should judge if they are matched. The first stri ...

  7. 2017ACM暑期多校联合训练 - Team 8 1006 HDU 6138 Fleet of the Eternal Throne (字符串处理 AC自动机)

    题目链接 Problem Description The Eternal Fleet was built many centuries ago before the time of Valkorion ...

  8. 2017ACM暑期多校联合训练 - Team 8 1002 HDU 6134 Battlestation Operational (数论 莫比乌斯反演)

    题目链接 Problem Description The Death Star, known officially as the DS-1 Orbital Battle Station, also k ...

  9. 2017ACM暑期多校联合训练 - Team 8 1011 HDU 6143 Killer Names (容斥+排列组合,dp+整数快速幂)

    题目链接 Problem Description Galen Marek, codenamed Starkiller, was a male Human apprentice of the Sith ...

随机推荐

  1. 图文详解 IntelliJ IDEA 15 创建 Maven 构建的 Java Web 项目(使用 Jetty 容器)

    图文详解 IntelliJ IDEA 15 创建 maven 的 Web 项目 搭建 maven 项目结构 1.使用 IntelliJ IDEA 15 新建一个项目.  2.设置 GAV 坐标  3. ...

  2. Mybatis 点点滴滴

    相比 Hibernate ,Mybatis 实在是学习门槛低多了. 1 . 类属性和表字段的自动对应 当向数据库中插入一行数据时,<insert>标签中的占位符#{}中的占位符的值写 mo ...

  3. SQL入门之集合操作

    尽管可以在与数据库交互时一次只处理一行数据,但实际上关系数据库通常处理的都是数据的集合.在数学上常用的集合操作为:并(union),交(intersect),差(except).对于集合运算必须满足下 ...

  4. HDU4466_Triangle

    今天比赛做的一个题目,不过今天终于感受到了复旦题目有多坑了. 题目的意思是给你一段长为n个单位长度的直线,你可以选择任意连续单位长度的线段组成三角形,可以组成任意你可以组成任意多个三角形,且要求其中所 ...

  5. bzoj4569-萌萌哒

    题目 有一个长度为\(n\)的十进制数,用\(s\)表示.有\(m\)个限制条件,每个条件形如:\((l_1,r_1,l_2,r_2)\),表示\(s[l_1:r_1]=s[l_2:r_2]\). 现 ...

  6. noip模拟题《迷》enc

    [问题背景]zhx 和他的妹子聊天.[问题描述]     考虑一种简单的加密算法.     假定所有句子都由小写英文字母构成, 对于每一个字母, 我们将它唯一地映射到另一个字母.例如考虑映射规则:a- ...

  7. 【Java】关于@RequestBody

    首先@RequestBody需要接的参数是一个string化的json,这里直接使用JSON.stringify(json)这个方法来转化 其次@RequestBody,从名称上来看也就是说要读取的数 ...

  8. [二十]SpringBoot 之 (多)文件上传

    (1)新建maven Java project 新建一个名称为spring-boot-fileuploadmaven java项目 (2)在pom.xml加入相应依赖: <project xml ...

  9. 洛谷 P2664 树上游戏 解题报告

    P2664 树上游戏 题目描述 \(\text{lrb}\)有一棵树,树的每个节点有个颜色.给一个长度为\(n\)的颜色序列,定义\(s(i,j)\) 为 \(i\) 到 \(j\) 的颜色数量.以及 ...

  10. 20135239益西拉姆 Linux内核分析 操作系统是怎样工作的?

    益西拉姆+ 原创作品+ <Linux内核分析>MOOC课程http://mooc.study.163.com/course/USTC-1000029000 ” 堆栈 堆栈是C语言程序运行时 ...