HDU 6103 Kirinriki【尺取法】【思维题】【好题】
来源:互联网 发布:孙玉伯 知乎 编辑:程序博客网 时间:2024/06/05 23:00
Kirinriki
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 456 Accepted Submission(s): 160
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.
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
Each case begins with one line with one integers m : the limit distance of substring.
Then a string S follow.
Limits
Each character in the string is lowercase letter,
Output
For each test case output one interge denotes the answer : the maximum length of the substring.
Sample Input
15abcdefedcb
Sample Output
5Hint[0, 4] abcde[5, 9] fedcbThe distance between them is abs('a' - 'b') + abs('b' - 'c') + abs('c' - 'd') + abs('d' - 'e') + abs('e' - 'f') = 5
Source
2017 Multi-University Training Contest - Team 6
题意:
给出字符串s,寻找其两个长度相同且不重叠的子串,满足其每位的ascil差值之和不大于m,且长度最长。
思路:
因为字符串的长度为5000,所以可以枚举两个子串的对称轴,然后对其区间进行尺取法,这样只需要o(n^2)的复杂度即可。
#include <iostream>#include <cstdio>#include <cstring>#include <string>#include <cmath>#include <algorithm>#include <queue>#include <map>using namespace std;const int INF = 0x3f3f3f3f;const int maxn = 5010;char a[maxn], num[maxn];int m;int solve(int len){int s = 0, t = 0, ans = 0, sum = 0;while (1){while (sum + num[t] <= m && t < len){sum += num[t];ans = max(t - s + 1, ans);t++;}sum -= num[s++];if (s >= len) break;}return ans;}int main(){int t;scanf("%d", &t);while (t--){int len, res = 0;scanf("%d", &m);scanf("%s", a);len = strlen(a);for (int i = 0; i <= len; i++)//枚举折的中点{int cnt = 0;for (int j = 1; j + i < len && i - j >= 0; j++)//枚举奇数情况num[cnt++] = abs(a[j + i] - a[i - j]);res = max(res, solve(cnt));}for (int i = 0; i <= len; i++)//枚举折的中点{int cnt = 0;for (int j = 1; j + i - 1 < len && i - j >= 0; j++)//枚举偶数情况num[cnt++] = abs(a[j + i - 1] - a[i - j]);res = max(res, solve(cnt));}printf("%d\n", res);}return 0;}
阅读全文
3 0
- HDU 6103 Kirinriki【尺取法】【思维题】【好题】
- HDU 6103 Kirinriki(尺取法 思维)
- 2017多校六 1008题 hdu 6103 Kirinriki 尺取法
- -----尺取法 hdu 6103-Kirinriki
- HDU 6103 Kirinriki (尺取法)
- HDU 6103 Kirinriki(尺取法)
- HDU 6103 Kirinriki(尺取法)
- hdu 6103 Kirinriki(尺取法)
- hdu 6103 Kirinriki(尺取法)
- HDU 6103 Kirinriki(尺取法)
- 【思维】hdu 6103 Kirinriki
- HDU 6103 Kirinriki(思维尺取)
- hdu 6103 Kirinriki dp+二分 或 尺取法
- hdu6103-2017多校6&尺取法|思维-Kirinriki
- HDU_6103 Kirinriki 【尺取法】
- HDU 6103 Kirinriki 枚举中间点(思维)+ 尺取
- HDU 6103 Kirinriki (字符串翻转 尺取法 17多校第六场)
- hdu6103 Kirinriki(尺取法)
- nlp中的LSTM学习
- Java反射
- Java web学习笔记之servlet技术
- 单调队列学习
- 编辑距离问题
- HDU 6103 Kirinriki【尺取法】【思维题】【好题】
- Oracl安装在Windows中
- js中的原型
- 详细了解步进电机的最大静转矩以及矩频特性
- Bomb Game
- 为什么存储过程比sql语句效率高?
- 深入理解Spring系列之八:常用的扩展接口
- jar包部署到Linux服务器
- sqlserver