题目
内容
给一个字符串,找到最长的子串,这个串经过重排,可以形成回文串.输出最长串的长度.
字串长度
n
≤
3
e
5
n\le 3e^5
n≤3e5,所有字符为小写字母,且属于
a
a
a到
t
t
t
分析
能构成回文串充要条件: 数量为奇数的字符种类小于等于1 .
由于字符种类最多20种,因此我们可以考虑状压,即: 第
i
i
i位表示第
i
i
i种字符的情况,如果为奇数个则为1,偶数个则为0.
我们统计每一种状态第一次出现位置,即可.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
typedef vector<int> vi;
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
const int mod = 1e9+7;
const int maxn = 4e6+10;
const int inf = 0x3f3f3f3f;
int pos[maxn];
char str[maxn];
int main()
{
ios::sync_with_stdio(false);cin.tie(0);
int n;
cin>>n;
cin>>str;
int now=0;
memset(pos,-1,sizeof(pos));
pos[0]=0;
int ans=1;
rep(i,0,n-1) {
int x=str[i]-'a';
now=now^(1<<x);
if(pos[now]==-1) pos[now]=i+1;
else ans=max(ans,i+1-pos[now]);
rep(j,0,19) {
int xx=now^(1<<j);
if(pos[xx]!=-1) ans=max(ans,i+1-pos[xx]);
}
}
cout<<ans<<'\n';
return 0;
}