【尺取】hdu 6103 Kirinriki

Link:http://acm.split.hdu.edu.cn/showproblem.php?pid=6103

#include <bits/stdc++.h>
using namespace std;

const int N = 5005;
char s[N];
int n,len;
int Ma(int x){
    int ans = 0,res = 0;
    int l = 1,r = 1;
    while(x-l>=0 && x+l<len){
        ans = ans + abs(s[x-l]-s[x+l]);
        while(ans > n && x-r>=0 && x+r<len){
            ans = ans - abs(s[x-r]-s[x+r]);
            r++;
        }
        l++;
        res = max(res,l-r);
    }
//    printf("Ma[%d] %d\n",x,res);
    return res;
}
int Mb(int x){
    int ans = 0,res = 0;
    int l = 1,r = 1;
    while(x-l+1>=0 && x+l<len){
        ans = ans + abs(s[x-l+1]-s[x+l]);
        while(ans > n && x-r+1>=0 && x+r<len){
            ans = ans - abs(s[x-r+1]-s[x+r]);
            r++;
        }
        l++;
        res = max(res,l-r);
    }
//    printf("Mb[%d] %d\n",x,res);
    return res;
}
int main(){
    int T;
    scanf("%d",&T);
    while(T--){
        scanf("%d",&n);
        scanf("%s",s);
        len = strlen(s);
        int res = 0;
        for(int i = 0; i < len; i++){
            res = max(res,max(Ma(i),Mb(i)));
        }
        printf("%d\n",res);
    }
    return 0;
}

/*
10
4
abcdeabcde
5
abcdeabcde
11
abcdeabcde
12
abcdeabcde
*/


發佈了317 篇原創文章 · 獲贊 11 · 訪問量 10萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章