BZOJ2213

# 题解

#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
#include<cctype>
#include<cmath>
#include<vector>
#include<map>
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define mp(a,b) make_pair<int,int>(a,b)
#define cls(s) memset(s,0,sizeof(s))
#define cp pair<int,int>
#define LL long long int
using namespace std;
const int maxn = 1000005,maxm = 100005,INF = 1000000000;
int out = 0,flag = 1; char c = getchar();
while (c < 48 || c > 57){if (c == '-') flag = -1; c = getchar();}
while (c >= 48 && c <= 57){out = (out << 3) + (out << 1) + c - 48; c = getchar();}
return out * flag;
}
vector<int> pos[26];
int n,ans;
void work(int x,int y){
int nowx = 0,nowy = 0,minx = INF,miny = INF,mnx = 0,mny = 0;
unsigned int px = 0,py = 0;
while (px < pos[x].size() || py < pos[y].size()){
if (py == pos[y].size() || (px < pos[x].size() && pos[x][px] < pos[y][py])){
miny = min(miny,mny);
nowx++; nowy--;
px++;
}
else {
minx = min(minx,mnx);
nowy++; nowx--;
py++;
}
mnx = min(nowx,mnx); mny = min(mny,nowy);
if (minx < INF) ans = max(ans,nowx - minx);
if (miny < INF) ans = max(ans,nowy - miny);
}
}
int main(){
n = read(); int x; char c = getchar();
while (!isalpha(c)) c = getchar();
for (int i = 1; i <= n; i++){
x = c - 'a';
pos[x].push_back(i);
c = getchar();
}
for (int x = 0; x < 26; x++)
for (int y = x + 1; y < 26; y++)
work(x,y);
printf("%d\n",ans);
return 0;
}


posted @ 2018-06-23 18:34  Mychael  阅读(131)  评论(0编辑  收藏  举报