HDU 6103 Kirinriki
来源:互联网 发布:python可以做界面吗 编辑:程序博客网 时间:2024/06/05 13:34
We define the distance of two strings A and B with same length n is
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不超过m的情况下的长度的最大值。
思路:一开始怎么想怎么都想不出来
这个官方题解说题目名字给提示,表示根本不认识这题目是啥啊。
#include <iostream>#include <stdio.h>#include <string.h>#include <queue>#include <cmath>#include <stdlib.h>using namespace std;typedef long long LL;const int MAXN = 5000+10;int ans, m, len;char s[MAXN];int main(){ int t; scanf("%d", &t); while(t--) { scanf("%d", &m); scanf("%s", s); len = strlen(s); ans = 0; //枚举中间的对称点,真实存在 for(int i = 1; i < len; ++i) { int l1 = i-1,r1 = l1; int l2 = i+1,r2 = l2; int sum = 0; while(l1 >= 0 && l2 < len) { sum += abs(s[l1] - s[l2]); while(sum > m)sum -= abs(s[r1--] - s[r2++]); ans = max(ans,r1 - l1 + 1); //printf("i:%d r1:%d l1:%d ans:%d\n",i,r1,l1,ans); l1--; l2++; } } //枚举中间的对称空隙(点不存在) for(int i = 0; i < len; ++i) { int l1 = i,r1 = l1; int l2 = i+1,r2 = l2; int sum = 0; while(l1 >= 0 && l2 < len) { sum += abs(s[l1] - s[l2]); while(sum > m)sum -= abs(s[r1--] - s[r2++]); ans = max(ans,r1 - l1 + 1); l1--; l2++; } } printf("%d\n", ans); } return 0;}
- HDU 6103 Kirinriki
- HDU 6103 Kirinriki
- HDU 6103 Kirinriki
- HDU 6103 Kirinriki
- hdu 6103 -Kirinriki
- hdu 6103 Kirinriki
- HDU 6103 Kirinriki 【dp】
- hdu 6103 Kirinriki
- HDU 6103 Kirinriki
- Kirinriki(HDU 6103)
- HDU-6103 Kirinriki
- hdu 6103 Kirinriki
- HDU 6103Kirinriki
- hdu--6103--Kirinriki
- 【思维】hdu 6103 Kirinriki
- HDU 6103 Kirinriki
- HDU 6103-Kirinriki
- HDU 6103 Kirinriki
- 子序列翻转
- c++ template traits
- c++ primer第三章笔记
- kmp 之 next数组
- 软件测试人员的脑子里到底在想什么?
- HDU 6103 Kirinriki
- 开博
- Oracle 常用环境变量查询
- MONKOVEL-安卓端免费的小说阅读器 源码分享
- 【STL】list的简单剖析以及各种函数的实现
- HDU5047
- 【转载】 友盟社会化Android组件之第三方登录
- [BZOJ1013][JSOI2008]球形空间产生器sphere-高斯消元
- python3[爬虫实战] 爬虫之scrapy爬取传智播客(我的奋斗我的路)