跳到正文

题解

题解:P3318 [SDOI2015] 双旋转字符串

elainya_stars约 10 分钟
原文

这是 2026年2月6日 存档的第 1 版,不是当前版本。查看当前版本

文章信息与版本

文章信息

发布
2026年2月6日
收录
2026年2月6日
更新
2026年11月17日
篇幅
约 10 分钟
获赞
2
题目
P3318

版本

  1. v2当前
  2. v1

P3318 [SDOI2015] 双旋转字符串 题解

写在前面的废话

我!切!紫!了!

我写了个 N<MN<M 和 N>MN>M,数据一个没过,hack 过了(?),然后我一看讨论区说数据都是 N=MN=M 的,这啥啊

题意

给 nn 个长度为 lsls 的串 s1∼ns_{1 \sim n} 和 mm 个长度为 ltlt 的串 t1∼mt_{1 \sim m},求满足“把 sis_i 和 tjt_j 拼起来后的串是双旋转的”的 (i,j)(i,j) 对数。

双旋转

对于一个长度为偶数的串 SS,定义她的长度为 lenlen,∃k∈[1,len/2],Sk∼len/2+S1,k−1=Slen/2+1,len\exist k \in [1,len/2],S_{k \sim len/2}+S_{1,k-1}=S_{len/2+1,len}(其中,++ 表示字符串拼接),那么串 SS 是双旋转的。

思路

比字符串相等,哈希最快。但是题目中没有给定准确的 nn、mm、lsls、ltlt 的范围,所以不能直接定义二维数组。但是 n×lsn \times ls 和 m×ltm \times lt 肯定够,所以输入一个串搞一个哈希数组,这样就不会爆空间了。

如果 lsls 和 ltlt 拼起来不是偶数的话,不会有双旋转串,输出 00 结束。

接下来,大模拟!

Part 1:ls<ltls<lt 或 ls>ltls>lt

加载失败qwq

自己画的看懂掌声,但是! 当出现 ???hu vijhu 这种类型的时候,① 和 ② 会都被算一次。所以两个 ① 中的“遍历黑区”jj 的起始点需 +1+1,终止点需 −1-1。(只是懒得改图了)

把每种 jj 的情况求出来“要求的串”的哈希值塞 map 里,比如样例处理完就是这样。

C++
map["jos"的哈希值]=2;
map["vij"的哈希值]=1;
map["ijo"的哈希值]=1;

mapimap_i 是 ii 这个哈希值提供几次贡献,输出的时候遍历短的那个串,然后累加就可以了。

Part 2:ls=ltls=lt

暴力求每个 ss 串的 lsls 种排列,所有排列塞进 map(这里 map 和上面用法是一样的),然后遍历 tt 串的时候累加 map 的哈希值贡献就可以了。

题外话,官方 Subtask 00 中都是 ls=ltls=lt。

时间复杂度 O(n×ls×log⁡n+m×lt×log⁡m)\mathcal{O}(n \times ls \times \log n+m \times lt \times \log m)。

Code

by elainya_stars
C++
#include<bits/stdc++.h>
using namespace std;
#define int long long
#define fastio ios::sync_with_stdio(0),cin.tie(0),cout.tie(0);
#define nbsp cerr<<'\n';
#define debugx(x) cerr<<#x<<" : "<<x<<'\n';
#define debuga(x,l,r) cerr<<#x<<" : ";for(int i=l;i<=r;i++)cerr<<x[i]<<' ';cerr<<'\n';
#define debugp(x,i) cerr<<#x<<'['<<i<<']'<<" : "<<x[i]<<'\n';
#define flin(s) char ccccc1[100]=#s;freopen(strcat(ccccc1,".in"),"r",stdin);
#define flout(s) char ccccc2[100]=#s;freopen(strcat(ccccc2,".out"),"w",stdout);
#define clr(x,y) memset(x,y,sizeof x);

const int N=4e6+5;
int n,m,ls,lt;
string s[N],t[N];
using ull=unsigned long long;
const ull base=1145141;
ull p[N],hs[N];
map<ull,int> mp;

ull cal_hs(int l,int r)
{
	if(l>r) // 一定要判断,因为会用到cal_hs(j+1,j)这种东西
		return 0;
	return hs[r]-hs[l-1]*p[r-l+1];
}

signed main()
{
	fastio;
	cin>>n>>m>>ls>>lt;
	if((ls+lt)%2!=0)
		return !(cout<<"0\n");
	p[0]=1;
	for(int i=1;i<=N-5;i++)
		p[i]=p[i-1]*base;
	for(int i=1;i<=n;i++)
		cin>>s[i],s[i]=" "+s[i];
	for(int i=1;i<=m;i++)
		cin>>t[i],t[i]=" "+t[i];
	if(ls>lt)
	{
		for(int i=1;i<=n;i++)
		{
			hs[0]=0;
			for(int j=1;j<=ls;j++)
				hs[j]=hs[j-1]*base+(ull)s[i][j];
			int l2=ls-(ls-lt)/2+1,r2=ls;
			ull now=cal_hs(l2,r2);
			for(int j=2;j+(r2-l2+1)-1<l2-1;j++) // 起始终止点分别需+1-1
				if(now==cal_hs(j,j+(r2-l2+1)-1))
				{
					ull h1=cal_hs(j+(r2-l2+1),l2-1);
					ull h2=cal_hs(1,j-1);
					ull cur=h1*p[j-1]+h2; // 计算h1+h2时注意将h1乘base的次方
					mp[cur]++;
				}
			for(int j=1;j+((ls+lt)/2-(r2-l2+1))-1<=l2-1;j++)
			{
				ull h1=cal_hs(j+((ls+lt)/2-(r2-l2+1)),l2-1);
				ull h2=cal_hs(1,j-1);
				ull cur=h1*p[j-1]+h2; // 计算h1+h2时注意将h1乘base的次方
				if(cur==now)
					mp[cal_hs(j,j+((ls+lt)/2-(r2-l2+1))-1)]++;
			}
		}
		int ans=0;
		for(int i=1;i<=m;i++)
		{
			hs[0]=0;
			for(int j=1;j<=lt;j++)
				hs[j]=hs[j-1]*base+(ull)t[i][j];
			ull now=cal_hs(1,lt);
//			cout<<now<<'\n';
			if(mp.count(now)) // 累加map中存储的贡献值
				ans+=mp[now];
		}
		cout<<ans<<'\n';
	}
	else if(ls<lt)
	{
		for(int i=1;i<=m;i++)
		{
			hs[0]=0;
			for(int j=1;j<=lt;j++)
				hs[j]=hs[j-1]*base+(ull)t[i][j];
			int l2=1,r2=1+(lt-ls)/2-1;
			ull now=cal_hs(l2,r2);
			for(int j=r2+2;j+(r2-l2+1)-1<lt;j++) // 起始终止点分别需+1-1
				if(now==cal_hs(j,j+(r2-l2+1)-1))
				{
					ull h1=cal_hs(j+(r2-l2+1),lt);
					ull h2=cal_hs(r2+1,j-1);
					ull cur=h1*p[(j-1)-(r2+1)+1]+h2; // 计算h1+h2时注意将h1乘base的次方
					mp[cur]++;
				}
			for(int j=r2+1;j+((lt+ls)/2-(r2-l2+1))-1<=lt;j++)
			{
				ull h1=cal_hs(j+((lt+ls)/2-(r2-l2+1)),lt);
				ull h2=cal_hs(r2+1,j-1);
				ull cur=h1*p[(j-1)-(r2+1)+1]+h2; // 计算h1+h2时注意将h1乘base的次方
				if(cur==now)
					mp[cal_hs(j,j+((lt+ls)/2-(r2-l2+1))-1)]++;
			}
		}
		int ans=0;
		for(int i=1;i<=n;i++)
		{
			hs[0]=0;
			for(int j=1;j<=ls;j++)
				hs[j]=hs[j-1]*base+(ull)s[i][j];
			ull now=cal_hs(1,ls);
//			cout<<now<<'\n';
			if(mp.count(now)) // 累加map中存储的贡献值
				ans+=mp[now];
		}
		cout<<ans<<'\n';
	}
	else
	{
		for(int i=1;i<=n;i++)
		{
			hs[0]=0;
			for(int j=1;j<=ls;j++)
				hs[j]=hs[j-1]*base+(ull)s[i][j];
			for(int j=1;j<=ls;j++)
			{
				int h1=cal_hs(j,ls); // 两边
				int h2=cal_hs(1,j-1);
				int now=h1*p[j-1]+h2; // 拼的时候注意乘base次方
				mp[now]++; // 全塞map里
			}
		}
		int ans=0;
		for(int i=1;i<=m;i++)
		{
			hs[0]=0;
			for(int j=1;j<=lt;j++)
				hs[j]=hs[j-1]*base+(ull)t[i][j];
			ull now=cal_hs(1,lt);
//			cout<<now<<'\n';
			if(mp.count(now)) // 累加map中存储的贡献值
				ans+=mp[now];
		}
		cout<<ans<<'\n';
	}
	return (0.0);
}

给我赞赞 qwq