hdu 6103 (尺取法)
来源:互联网 发布:无限网络万能钥匙 编辑:程序博客网 时间:2024/04/30 05:43
We define the distance of two strings A and B with same length n is
disA,B=∑i=0n−1|Ai−Bn−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.
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.
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,
15abcdefedcb
5
[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
很巧妙地尺取。。。从前面枚举总的长度,然后尺取当前长度所能产生的最长的可行串,具体方法是取中点,然后延伸可行长度,更新最大值,不可行的时候删除最前端的影响,其实就是固定了起点和终点进行暴力枚举
一开始想的是用尺取法移串可是这样复杂度太高了 重复算太多了,也不好操作
这里固定了起点和终点然后每次枚举就变成on了
#include <bits/stdc++.h>using namespace std;const int maxn = 5010;char s[maxn];int ans=0;int len,m;void solve(){ for(int i=2;i<=len;i++) { int h=i/2; int sum=0; int l=0; for(int j=0;j<h;j++) { sum+=abs(s[j]-s[i-j-1]); while(sum>m&&l<j) { sum-=abs(s[l]-s[i-l-1]); l++; } if(sum<=m) ans=max(ans,j-l+1); } }}int main(){ int t; scanf("%d",&t); while(t--) { scanf("%d",&m); scanf("%s",s); len = strlen(s); ans=0; solve(), reverse(s,s+len); solve(); printf("%d\n",ans ); } return 0;}
阅读全文
0 0
- hdu 6103 (尺取法)
- 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 5358(尺取法)
- hdu 5672(尺取法)
- hdu 5358(尺取法)
- HDU 5289 Assignment(尺取法)
- HDU 5338. First One(尺取法)
- HDU 5672 String(尺取法)
- hdu 5056 Boring count(尺取法)
- 数列问题 HDU 5806 (尺取法)
- hdu 5178 pairs(尺取法求解)
- 破解常用断点设置
- docker registry带ssl认证的私有仓库搭建
- 图论--最短路问题--Bellman-Ford算法
- 递归求累加和
- python中排序方法的自定义
- hdu 6103 (尺取法)
- 深度学习基础知识(5):池化层的实现
- jsp之间传参
- 高级网络
- spring 之 Listener的异步调用
- Android如何设置TextView的行间距、行高
- 【题解】vijos1093 文科生的烦恼
- 笔记11:进程控制编程
- HDU