AtCoder Regular Contest 099 题解

8 篇文章 0 订阅

题意:

给出一个操作序列包含 &lt; &gt; + − &lt;&gt;+- <>+,分别是下标左移右移,当前位置加减。问有多少对 ( i , j ) (i,j) (i,j)满足只做这里面的操作,结果和做完所有操作一样。

题解:

很神奇的一道题。
首先将操作后得到的序列看成一个多项式: T ( S ) = ∑ i = − 1 0 9 1 0 9 a i x i T(S)=\sum_{i=-10^9}^{10^9}a_ix^i T(S)=i=109109aixi
考虑在操作序列前加操作,多项式会变成什么
t ( &gt; S ) = T ( S ) x t(&gt;S)=T(S)x t(>S)=T(S)x

t ( &lt; S ) = T ( S ) x − 1 t(&lt;S)=T(S)x^{-1} t(<S)=T(S)x1

t ( + S ) = T ( S ) + 1 t(+S)=T(S)+1 t(+S)=T(S)+1

t ( − S ) = T ( S ) − 1 t(-S)=T(S)-1 t(S)=T(S)1

给整个多项式一个hash值c,那么就是问多少对 ( i , j ) (i,j) (i,j)满足这样得出来的序列的hash等于c。
这四个操作显然是可逆的,设 ( i , j ) (i,j) (i,j)中的操作为 t s i t s i + 1 t s i + 2 … t s j t_{s_i}t_{s_{i+1}}t_{s_{i+2}}…t_{s_{j}} tsitsi+1tsi+2tsj逆操作为 t s i − 1 t s i + 1 − 1 t s i + 2 − 1 … t s j − 1 t_{s_i}^{-1}t_{s_{i+1}}^{-1}t_{s_{i+2}}^{-1}…t_{s_{j}}^{-1} tsi1tsi+11tsi+21tsj1
t s i t s i + 1 t s i + 2 … t s j ( 0 ) = c t_{s_i}t_{s_{i+1}}t_{s_{i+2}}…t_{s_{j}}(0)=c tsitsi+1tsi+2tsj(0)=c

t s n − 1 t s n − 1 − 1 t s n − 2 − 1 … t s i − 1 t s i t s i + 1 t s i + 2 … t s j ( 0 ) = t s n − 1 t s n − 1 − 1 t s n − 2 − 1 … t s i − 1 ( c ) t_{s_n}^{-1}t_{s_{n-1}}^{-1}t_{s_{n-2}}^{-1}…t_{s_{i}}^{-1}t_{s_i}t_{s_{i+1}}t_{s_{i+2}}…t_{s_{j}}(0)=t_{s_n}^{-1}t_{s_{n-1}}^{-1}t_{s_{n-2}}^{-1}…t_{s_{i}}^{-1}(c) tsn1tsn11tsn21tsi1tsitsi+1tsi+2tsj(0)=tsn1tsn11tsn21tsi1(c)

t s n − 1 t s n − 1 − 1 t s n − 2 − 1 … t s j + 1 − 1 ( 0 ) = t s n − 1 t s n − 1 − 1 t s n − 2 − 1 … t s i − 1 ( c ) t_{s_n}^{-1}t_{s_{n-1}}^{-1}t_{s_{n-2}}^{-1}…t_{s_{j+1}}^{-1}(0)=t_{s_n}^{-1}t_{s_{n-1}}^{-1}t_{s_{n-2}}^{-1}…t_{s_{i}}^{-1}(c) tsn1tsn11tsn21tsj+11(0)=tsn1tsn11tsn21tsi1(c)
变成两个后缀形式,可以线性求出hash值,map统计即可。
自己写的怎么都过不了,各种hash被卡,抄了别人写法
WA:

#include<map>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#define LL long long
#define MP make_pair
using namespace std;
const LL mod=1e9+7;
LL base[4],pre[4][500010],inv[4];
struct node{LL a,b;}op[4][250010];
map<pair<pair<LL,LL>,pair<LL,LL> >,LL> mp;
LL pow(LL a,LL b)
{
	LL ans=1;
	while(b)
	{
		if(b&1) ans=ans*a%mod;
		a=a*a%mod;b>>=1;
	}
	return ans;
}
LL n,p=0,t[500010],c[4],a[4][250010],b[4][250010];
char s[250010];
void Pre()
{
	for(int k=0;k<4;k++)
	{
		LL cnt=1;op[k][n+1].a=1;
		for(LL i=n;i>=1;i--)
		{
			op[k][i]=op[k][i+1];
			if(s[i]=='<') cnt=cnt*base[k]%mod,(op[k][i].a*=base[k])%=mod;
			if(s[i]=='>') cnt=cnt*inv[k]%mod,(op[k][i].a*=inv[k])%=mod;
			if(s[i]=='-') (op[k][i].b+=cnt*pre[k][n]%mod)%=mod;
			if(s[i]=='+') op[k][i].b=((op[k][i].b-cnt*pre[k][n]%mod)%mod+mod)%mod;
		}
	}
}
void solve(LL *a,LL c,LL k) {for(LL i=1;i<=n;i++) a[i]=(c*op[k][i].a%mod+op[k][i].b)%mod;}
int main()
{
	base[0]=2333;base[1]=10037;base[2]=19260817;base[3]=998244353;
	for(int i=0;i<4;i++) inv[i]=pow(base[i],mod-2);
	scanf("%lld",&n);getchar();
	scanf("%s",s+1);
	for(LL k=0;k<4;k++)
	{
		pre[k][0]=1;for(LL i=1;i<=2*n;i++) pre[k][i]=pre[k][i-1]*base[k]%mod;
	}
	for(LL i=1;i<=n;i++)
	{
		if(s[i]=='<') p--;if(s[i]=='>') p++;
		if(s[i]=='-') t[p+n]--;if(s[i]=='+') t[p+n]++;
	}
	for(int k=0;k<4;k++)
		for(LL i=-n;i<=n;i++) (c[k]+=pre[k][i+n]*t[i+n]%mod)%=mod;
	Pre();
	for(int i=0;i<4;i++) solve(a[i],0,i);
	for(int i=0;i<4;i++) solve(b[i],c[i],i);
	LL ans=0;
	for(LL i=2;i<=n+1;i++)
	{
		mp[MP(MP(b[0][i-1],b[1][i-1]),MP(b[2][i-1],b[3][i-1]))]++;
		ans+=mp[MP(MP(a[0][i],a[1][i]),MP(a[2][i],a[3][i]))];
	}
	printf("%lld",ans);
}

AC:

#include<map>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
#define LLu unsigned long long
using namespace std;
char s[250010];
LLu base=233,inv=7204522363551799129,f[250010],X=1,Y,ans=0;
int n;
map<LLu,int> mp;
int main()
{
	scanf("%d",&n);getchar();
	scanf("%s",s+1);
	for(int i=1;i<=n;i++)
	{
		f[i]=f[i-1];
		if(s[i]=='+') f[i]+=X;
		if(s[i]=='-') f[i]-=X;
		if(s[i]=='<') X*=base;
		if(s[i]=='>') X*=inv;
		mp[f[i]]++;
	}
	X=1;
	for(int i=1;i<=n;i++)
	{
		ans+=mp[f[n]*X+Y];
		if(s[i]=='+') Y+=X;
		if(s[i]=='-') Y-=X;
		if(s[i]=='<') X*=base;
		if(s[i]=='>') X*=inv;
		mp[f[i]]--;
	}
	printf("%llu",ans);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值