Jerry的挑战

问题概述: 有一天Jerry给Tom出了一道题来考验他。 Jerry给了Tom一个长度为2*n的只包含小写字母的字符串,让Tom将这个字符串任意挑选字符,将其分成两个等长的字符串a和b(对于一个si不能同时被选到a和b中),然后a要和reverse(b)相同(a和反转后的b相同),问这样的方案数有多少?Tom有些为难,所以请你来帮帮他吧。

输入一个正整数n,和一个长度为2*n的字符串。

输出方案数。

示例1 输入: 2 "abba" 输出: 4

 

#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int Maxn=45;
const ll e1=233,p1=1e9+7,e2=131,p2=998244353;
ll f1[Maxn]={1},f2[Maxn]={1};
char s[Maxn];
int n,tot;
map<pair<ll,ll>,ll>Count;
int main(){
	scanf("%d%s",&n,s+1);
	for(int i=1;i<=n;++i)
		f1[i]=f1[i-1]*e1%p1,f2[i]=f2[i-1]*e2%p2;
	tot=(1<<n)-1;
	for(int i=0;i<=tot;++i){
		ll _h0=0,_h1=0,h0_=0,h1_=0,st=0,ed=n;
		for(int j=0;j<n;++j)
			if(i&(1<<j)){
				++st;
				(_h1+=f1[st]*(s[j+1]-'a'+1)%p1)%=p1;
				(h1_+=f2[st]*(s[j+1]-'a'+1)%p2)%=p2;
			}else {
				(_h0+=f1[ed]*(s[j+1]-'a'+1)%p1)%=p1;
				(h0_+=f2[ed]*(s[j+1]-'a'+1)%p2)%=p2;
				--ed;
			}
		++Count[make_pair(_h1-_h0,h1_-h0_)];
	}
	long long ans=0;
	for(int i=0;i<=tot;++i){
		ll _h0=0,_h1=0,h0_=0,h1_=0,st=n-__builtin_popcount(i),ed=st;
		for(int j=0;j<n;++j)
			if(i&(1<<j)){
				++st;
				(_h1+=f1[st]*(s[n+j+1]-'a'+1)%p1)%=p1;
				(h1_+=f2[st]*(s[n+j+1]-'a'+1)%p2)%=p2;
			}else {
				(_h0+=f1[ed]*(s[n+j+1]-'a'+1)%p1)%=p1;
				(h0_+=f2[ed]*(s[n+j+1]-'a'+1)%p2)%=p2;
				--ed;
			}
		ans+=Count[make_pair(_h0-_h1,h0_-h1_)];
	}
	cout<<ans/2<<endl;
	return 0;

}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值