题解:P3318 [SDOI2015] 双旋转字符串
elainya_stars
·
·
题解
P3318 [SDOI2015] 双旋转字符串 题解
:::info[写在前面的废话]
我!切!紫!了!
我写了个 N<M 和 N>M,数据一个没过,hack 过了(?),然后我一看讨论区说数据都是 N=M 的,这啥啊
:::
题意
给 n 个长度为 ls 的串 s_{1 \sim n} 和 m 个长度为 lt 的串 t_{1 \sim m},求满足“把 s_i 和 t_j 拼起来后的串是双旋转的”的 (i,j) 对数。
:::info[双旋转]{open}
对于一个长度为偶数的串 S,定义她的长度为 len,\exist k \in [1,len/2],S_{k \sim len/2}+S_{1,k-1}=S_{len/2+1,len}(其中,+ 表示字符串拼接),那么串 S 是双旋转的。
:::
思路
比字符串相等,哈希最快。但是题目中没有给定准确的 n、m、ls、lt 的范围,所以不能直接定义二维数组。但是 n \times ls 和 m \times lt 肯定够,所以输入一个串搞一个哈希数组,这样就不会爆空间了。
如果 ls 和 lt 拼起来不是偶数的话,不会有双旋转串,输出 0 结束。
接下来,大模拟!
:::info[Part 1:ls<lt 或 ls>lt]{open}
自己画的看懂掌声,但是! 当出现 ???hu vijhu 这种类型的时候,① 和 ② 会都被算一次。所以两个 ① 中的“遍历黑区”j 的起始点需 +1,终止点需 -1。(只是懒得改图了)
把每种 j 的情况求出来“要求的串”的哈希值塞 map 里,比如样例处理完就是这样。
map["jos"的哈希值]=2;
map["vij"的哈希值]=1;
map["ijo"的哈希值]=1;
:::
:::info[Part 2:$ls=lt$]{open}
暴力求每个 $s$ 串的 $ls$ 种排列,所有排列塞进 map(这里 map 和上面用法是一样的),然后遍历 $t$ 串的时候累加 map 的哈希值贡献就可以了。
题外话,官方 Subtask $0$ 中都是 $ls=lt$。
:::
时间复杂度 $O(n \times ls \times \log n+m \times lt \times \log m)$。
## Code
:::success[by elainya_stars]
```cpp
#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