HDU 6103 Kirinriki(思维尺取)

来源:互联网 发布:mac打开文件夹命令 编辑:程序博客网 时间:2024/05/17 02:50

题意:在一个字符串中,选取两个长度相同且不重合的子串,计算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 _ %MODtypedef 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;}
原创粉丝点击