Beer Mugs

题目

题目链接

内容

给一个字符串,找到最长的子串,这个串经过重排,可以形成回文串.输出最长串的长度.
字串长度 n ≤ 3 e 5 n\le 3e^5 n3e5,所有字符为小写字母,且属于 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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值