HDU 6103 Kirinriki(尺取法)

来源:互联网 发布:谷歌地图软件 编辑:程序博客网 时间:2024/05/17 02:04

题目地址
题意:给你一个字符串,把它尽可能的分割出两个长度相同的字符串,并且两个字符串的dis要小于等于m,求出这个长度。
dis的求法:这里写图片描述
思路:因为字符串的长度之和只有20000,所以可以枚举中心点,然后对于每个条件通过尺取法去求出符合条件的长度

#include <iostream>#include <cstring>#include <string>#include <queue>#include <vector>#include <map>#include <set>#include <stack>#include <cmath>#include <cstdio>#include <algorithm>#define N 200010#define LL __int64#define inf 0x3f3f3f3f#define lson l,mid,ans<<1#define rson mid+1,r,ans<<1|1#define getMid (l+r)>>1#define movel ans<<1#define mover ans<<1|1using namespace std;string str;int ans, m;int len;void solve() {    for (int i = 2; i <= str.length(); i++) {//遍历每个长度        int lens = i / 2;        int l = 0, n = 0, sum = 0;        for (int j = 0; j < lens; j++) {//遍历每个左端起点            sum += abs(str[j] - str[i - j - 1]);            if (sum <= m) {                n++;                ans = max(ans, n);            }            else {                n--;                sum -= abs(str[j] - str[i - j - 1]);//还原回去上一个状态                sum -= abs(str[l] - str[i - l - 1]);//尺取法向中心移动一位                l++;                j--;            }        }    }}int main() {    cin.sync_with_stdio(false);    int T;    cin >> T;    while (T--) {        cin >> m;        cin >> str;        ans = 0;        len = str.length();        solve();        reverse(str.begin(), str.end());        solve();        cout << ans << endl;    }    return 0;}
原创粉丝点击