2017 Multi-University Training Contest - Team 6 HDU 6103 Kirinriki (尺取法)
題目鏈接:
HDU 6103 Kirinriki
題目大意:
給一個字符串s,和一個值m.
定義
在s串中找出最長的AB子串使得
解題思路:
比賽的時候這道題給的數據是
首先可以枚舉一個對稱點, 然後用尺取法往外擴張,考慮兩種情況, 中間的留一個字母和中間不留字母。
代碼:
/**********************************************
*Author* :ZZZZone
*reated Time* : 2017/8/11 13:47:16
*ended Time* : 2017/8/11 14:07:47
*********************************************/
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <stack>
using namespace std;
typedef pair<int, int> PII;
typedef long long LL;
typedef unsigned long long ULL;
const int MaxN = 5e3;
int T, m, ans, len;
char s[MaxN + 5];
void check(int p){
int r = p;
int tot = 0;
for(int i = p; i >= 0; i--){
int tmp = 2 * p + 1;
if(tmp - i >= len) break;
tot += abs(s[i] - s[tmp - i]);
while(tot > m && r >= i){
tot -= abs(s[r] - s[tmp - r]);
r--;
}
ans = max(ans, r - i + 1);
}
tot = 0, r = p - 1;
for(int i = p - 1; i >= 0; i--){
int tmp = 2 * p;
if(tmp - i >= len) break;
tot += abs(s[i] - s[tmp - i]);
while(tot > m && r >= i){
tot -= abs(s[r] - s[tmp - r]);
r--;
}
ans = max(ans, r - i + 1);
}
}
int main()
{
scanf("%d", &T);
while(T--){
scanf("%d", &m);
scanf("%s", s);
ans = 0; len = strlen(s);
for(int i = 0; i < len; i++) check(i);
printf("%d\n", ans);
}
return 0;
}