HDU 6103 Kirinriki(思维尺取)

41 篇文章 0 订阅
12 篇文章 0 订阅

题意:在一个字符串中,选取两个长度相同且不重合的子串,计算dis值,定义为abcde efcgh 的a~h+b~g+c~c+d~f..即计算对应位置的差值和.
给定m,找到满足dis值小于等于m的最大子串对长度.
关键:枚举对称轴.进行尺取
复杂度 O(m^2).m<=5000
事实上,在见到小于等于m的时候,要想到尺取,对应位置差值和,应想到枚举对称轴.处理时注意可能对称到空处,*2后分奇偶讨论.

/*  xzppp  */
#include <iostream>
#include <vector>
#include <cstdio>
#include <string.h>
#include <algorithm>
#include <queue>
#include <map>
#include <string>
#include <cmath>
#include <bitset>
#include <iomanip>
using namespace std;
#define FFF freopen("in.txt","r",stdin);freopen("out.txt","w",stdout);
#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
#define MP make_pair
#define PB push_back
#define _ %MOD
typedef long long  LL;
typedef unsigned long long ULL;
typedef pair<int,int > pii;
typedef pair<LL,LL> pll;
typedef pair<double,double > pdd;
typedef pair<double,int > pdi;
const int MAXM = 2e3+17;
const int MAXV = 2*1e3+17;
const int BIT = 15+3;
const int INF = 0x7fffffff;
const LL INFF = 0x3f3f3f3f3f3f3f3f;
const int MOD = 1e9+7;
const int MAXN = 1e5+17;
string str;
int main()
{    
        #ifdef GoodbyeMonkeyKing
    FFF
    #endif
    int t,x;
    cin>>t;
    x = t;
    while(t--)
    {
        int m;
        cin>>m>>str;
        int ans = 0;
        for (int i = 1; i < 2*str.length(); ++i)
        {
            int l = i/2-1,r=i/2+1;
            if(i&1) l++;
            int temp = 0,lth = 0;
            queue<int > q;
            while(l>-1&&r<str.length())
            {
                q.push(abs(str[r]-str[l]));
                temp += abs(str[r]-str[l]);
                lth++;
                if(temp>m&&!q.empty())
                {
                    temp -= q.front(); q.pop();
                    lth--;
                }
                ans = max(lth,ans);
                l--;r++;
            }
        }
        cout<<ans<<endl;
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值