Gym 100134H (状压+计数)

题意:
给出一个长为n的字符串,问有多少个可以重排列出回文串的子区间。


思路:
首先我们想到n^2的做法,预处理出0~n-1的全部字符的前缀和,枚举每个区间,通过前缀和O(1)计算出这个区间中各个字母的个数,当且仅当所有的字母出现次数中奇数的个数为0或1时,可以通过重排得到回文串。
我们思考一种O(nlogn)的做法:
1、因为字母个数只有奇偶有意义,所以将52个字母状压到一个long long里面。
2、通过XOR预处理出0~n-1的全部字符的前缀和,得到一个状压后的long long数组cnt。
3、现在状压数组中每一个数对应一个0~i的区间,任意两个数cnt[a],cnt[b] XOR之后的到的是对应区间[a-1,b]的状压结果res,当res==0||res==lowbit(res)时,[a-1,b]有贡献。
4、只有cnt[a]==cnt[b]的时候,res==0,所以将相同的cnt排序后合并,然后C(num[cnt[x]],2)就是对答案的贡献。如果cnt[x]==lowbit(cnt[x]),那么x可以和所有cnt[y]==0的y配对出一个有贡献的区间。所有cnt[x]==lowbit(cnt[x])或者cnt[x]==0的区间[0,x]本身就是一个贡献区间。
5、将cnt排序,枚举每一个值的每一种只有一个位为1的情况,设当前值为a,XOR后可以得到需要的值b,在cnt中二分找b,如果存在b,那么这样的区间对答案的贡献是 a出现次数*b出现次数。

复杂度O(52*nlogn)。


代码:

#include <iostream>
#include <iomanip>
#include <algorithm>
#include <cstring>
#include <cctype>
#include <cstdlib>
#include <cstdio>
#include <cmath>
#include <ctime>
#include <map>
#include <list>
#include <set>
#include <stack>
#include <queue>
#include <string>
#include <sstream>
#define pb push_back
#define X first
#define Y second
#define ALL(x) x.begin(),x.end()
#define INS(x) inserter(x,x.begin())
#define pii pair<int,int>
#define qclear(a) while(!a.empty())a.pop();
#define lowbit(x) (x&-x)
#define sd(n) scanf("%d",&n)
#define sdd(n,m) scanf("%d%d",&n,&m)
#define sddd(n,m,k) scanf("%d%d%d",&n,&m,&k)
#define mst(a,b) memset(a,b,sizeof(a))
#define cout3(x,y,z) cout<<x<<" "<<y<<" "<<z<<endl
#define cout2(x,y) cout<<x<<" "<<y<<endl
#define cout1(x) cout<<x<<endl
#define IOS std::ios::sync_with_stdio(false)
#define SRAND srand((unsigned int)(time(0)))
typedef long long ll;
typedef unsigned long long ull;
typedef unsigned int uint;
using namespace std;
const double PI=acos(-1.0);
const int INF=0x3f3f3f3f;
const ll mod=998244353;
const double eps=1e-8;
const int maxn=300005;
const int maxm=10005;

int n;
char str[maxn];
ll cnt[maxn];
map<ll,int>maps;
void solve() {
    sd(n);
    scanf("%s",str);
    int now;
    if(str[0]>='a'){
        now=(str[0]-'a');
    }else{
        now=(str[0]-'A'+26);
    }
    cnt[0]=1LL<<now;
    maps[cnt[0]]++;
    for(int i=1;i<n;i++){
        int now;
        if(str[i]>='a'){
            now=(str[i]-'a');
        }else{
            now=(str[i]-'A'+26);
        }
        cnt[i]=1LL<<now;
        cnt[i]=cnt[i-1]^cnt[i];
        maps[cnt[i]]++;
    }
    sort(cnt,cnt+n);
    n=unique(cnt,cnt+n)-cnt;
    ll ans=0;
    int cnt0=maps[0];
    for(auto it=maps.begin();it!=maps.end();it++){
        ll nowx=it->X;
        int nowy=it->Y;
        int nowcnt=0;
        while(nowx){
            if(nowx&1)nowcnt++;
            nowx>>=1;
        }
        if(nowcnt<=1){
            ans+=nowy;
        }
        if(nowcnt==1){
            ans+=(ll)cnt0*nowy;
        }
        if(nowy>=2)ans+=((ll)nowy*(nowy-1))>>1;
    }
    for(int i=0;i<n;i++){
        ll now=cnt[i];
        ll temp=1;
        while(temp<now){
            if(temp&now){
                ll x=now^temp;
                int pos=lower_bound(cnt,cnt+n,x)-cnt;
                if(pos==n)continue;
                if(cnt[pos]==x){
                    ans+=(ll)maps[x]*maps[now];
                }
            }
            temp<<=1;
        }
    }
    printf("%lld\n",ans);
    return ;
}
int main() {
#ifdef LOCAL
    freopen("in.txt","r",stdin);
//    freopen("out.txt","w",stdout);
#else
        freopen("hyperdrome.in","r",stdin);
        freopen("hyperdrome.out","w",stdout);
#endif
    solve();
    return 0;
}


 

猜你喜欢

转载自blog.csdn.net/weixin_38378637/article/details/81254362