【字符串】字符串哈希
课堂练习
-P8835 [传智杯 #3 决赛] 子串-
思路
- 纯暴力
Code
#include<bits/stdc++.h>
#define int long long
#define pii pair<int,int>
using namespace std;
const int N=1e6+10,maxl=1e3+10,inf=0x3f3f3f3f3f3f,mod=1e9+7,LOG=60;
int T,n,m;
string s,t;
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin>>T;
while(T--)
{
int ans=0;
cin>>n>>m;
cin>>s>>t;
//全部转小写
for(int i=0;i<s.size();i++)
{
if(s[i]>='A' && s[i]<='Z') s[i]=(char)s[i]+32;
}
for(int i=0;i<t.size();i++)
{
if(t[i]>='A' && t[i]<='Z') t[i]=(char)t[i]+32;
}
for(int i=0;i<m;i++)
{
if(t[i]==s[0])
{
string x=t.substr(i,s.size());
//字符串.substr(初始位置,需截取长度)
if(x==s) ans++;//统计答案
}
}
cout<<ans<<"\n";
}
return 0;
}-P10468 兔子与兔子-
思路
- 字符串哈希之子串哈希模板
数组定义
- 表示字符串前 个字符组成的前缀的哈希值
- 基底 的 次方,用于子串哈希的位数对齐
- 题目需要判断两个子串是否一样,那么就需要计算字符串中区间 的子串的哈希值
- 计算公式:
- 原理:前缀哈希是从左到右按高位计算的, 对应的是前 位的哈希值,乘以 相当于把它左移到和 相同的高位,再相减就得到了中间子串独立的哈希值
- 有了哈希值,我们现在就只需要判断两个子串的哈希值是否相等即可
Code
#include<bits/stdc++.h>
#define int unsigned long long
#define pii pair<int,int>
using namespace std;
const int N=1e6+10,maxl=1e3+10,inf=0x3f3f3f3f3f3f,mod=1e9+7;
const int p=131;
int n;
int hs[N],pw[N];
string s;
int get_hash(int l,int r)
{
return hs[r]-hs[l-1]*pw[r-l+1];
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin>>s>>n;
int len=s.size();
s='@'+s;
pw[0]=1;
for(int i=1;i<=len;i++)
{
hs[i]=hs[i-1]*p+s[i];
pw[i]=pw[i-1]*p;
}
while(n--)
{
int l1,r1,l2,r2;
cin>>l1>>r1>>l2>>r2;
int tmp1=get_hash(l1,r1),tmp2=get_hash(l2,r2);
cout<<((tmp1==tmp2)? "Yes":"No")<<"\n";
}
return 0;
}-CF39J Spelling Check-
思路
- 另一个子串哈希模板
数组定义
- 表示字符串前 个字符组成的前缀的哈希值
- 基底 的 次方,用于子串哈希的位数对齐
- 字符串 的总哈希值
int get_hash(int l,int r)
{
return hs[r]-hs[l-1]*pw[r-l+1];
}计算字符串 中区间子串 的哈希值,详见上一题
int del_hash(int l,int r,int len)
{
return get_hash(1,l-1)*pw[len-r]+get_hash(r+1,len);
}功能:计算字符串 删除区间 后,剩余字符串的哈希值
原理:将剩余字符串拆分为前半段 与后半段 ,把前半段的哈希值左移后半段长度位(乘以 ),再加上后半段的哈希值,完成两段的拼接,即可得到删除后的完整哈希
- 有了删除操作后,就只需遍历字符串 的每一个位置,计算删除该位置后的哈希值,若与字符串 的哈希相等,则该位置为合法删除位,加入答案,最后按题目要求输出即可
Code
#include<bits/stdc++.h>
#define int long long
#define pii pair<int,int>
using namespace std;
const int N=1e6+10,maxl=1e3+10,inf=0x3f3f3f3f3f3f,mod=1e9+7;
const int p=131;
int sum;
int hs[N],pw[N];
string s,t;
vector<int> ans;
int get_hash(int l,int r)
{
return hs[r]-hs[l-1]*pw[r-l+1];
}
int del_hash(int l,int r,int len)
{
return get_hash(1,l-1)*pw[len-r]+get_hash(r+1,len);
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin>>s>>t;
int len1=s.size(),len2=t.size();
s='@'+s;
t='@'+t;
pw[0]=1;
for(int i=1;i<=len1;i++)
{
hs[i]=hs[i-1]*p+s[i];
pw[i]=pw[i-1]*p;
}
for(int i=1;i<=len2;i++) sum=sum*p+t[i];
for(int i=1;i<=len1;i++)
{
int tmp=del_hash(i,i,len1);
if(tmp==sum) ans.push_back(i);
}
cout<<ans.size()<<"\n";
for(int x:ans) cout<<x<<" ";
return 0;
}-P3370 【模板】字符串哈希-
思路
Code-暴力
#include<bits/stdc++.h>
#define int long long
#define pii pair<int,int>
using namespace std;
const int N=1e6+10,maxl=1e3+10,inf=0x3f3f3f3f3f3f,mod=1e9+7,LOG=60;
int n,ans;
string s;
map<string,int> mp;
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin>>n;
while(n--)
{
cin>>s;
mp[s]++;
}
for(auto x:mp)
{
if(x.second!=0) ans++;
}
cout<<ans;
return 0;
}Code-手写哈希
#include<bits/stdc++.h>
#define int unsigned long long
#define pii pair<int,int>
using namespace std;
const int N=1e6+10,maxl=1e3+10,inf=0x3f3f3f3f3f3f,mod=1e6+7,LOG=60;
const int p=131;
int n,ans;
string s;
map<int,int> mp;
void init(string x)
{
int len=s.size();
s='@'+s;
int sum=0;
for(int i=1;i<=len;i++) sum=sum*p+s[i];
mp[sum]=1;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin>>n;
for(int i=1;i<=n;i++)
{
cin>>s;
init(s);
}
for(auto x:mp)
{
if(x.second!=0) ans++;
}
cout<<ans;
return 0;
}-P6739 [BalticOI 2014] Three Friends (Day1)-
思路
我们可以枚举被删掉的点 ,看剩下的字符串是否可以分成相同的两个字符串,分三种情况讨论点 :
- (删掉中点)
lt=get_hash(1,x-1);//取左半边哈希值
rt=get_hash(x+1,n);//取右半边哈希值- (删掉左半边里面的一个字符)
lt=del_hash(1,mid,x);//删除位置x之后剩下左半边字符串的哈希值
rt=get_hash(mid+1,n);//取右半边哈希值- (删掉右半边里面的一个字符)
lt=get_hash(1,mid-1);//取左半边哈希值
rt=del_hash(mid,n,x);//删除位置x之后剩下右半边字符串的哈希值Code
#include<bits/stdc++.h>
#define int long long
#define pii pair<int,int>
using namespace std;
const int N=2e6+10,maxl=1e3+10,inf=0x3f3f3f3f3f3f,mod=1e9+7;
const int p=13331;
int n,mid,idx,cnt;
int lt,rt;
int hs[N],pw[N];
string s;
map<int,int> vis;
int get_hash(int l,int r)
{
return hs[r]-hs[l-1]*pw[r-l+1];
}
int del_hash(int l,int r,int pos)
{
return get_hash(l,pos-1)*pw[r-pos]+get_hash(pos+1,r);
}
bool check(int x)
{
if(x==mid)
{
lt=get_hash(1,x-1);
rt=get_hash(x+1,n);
}
else if(x<mid)
{
lt=del_hash(1,mid,x);
rt=get_hash(mid+1,n);
}
else
{
lt=get_hash(1,mid-1);
rt=del_hash(mid,n,x);
}
return (lt==rt);
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin>>n>>s;
int len=s.size();
s='@'+s;
mid=(n+1)/2;
pw[0]=1;
for(int i=1;i<=len;i++)
{
hs[i]=hs[i-1]*p+s[i];
pw[i]=pw[i-1]*p;
}
for(int i=1;i<=n;i++)
{
if(check(i))//若删除i后字符串左右两边相等
{
idx=i;
if(vis[rt]>0) continue;//同一个位置只记录一次
vis[rt]=1;
cnt++;//统计答案
}
}
if(cnt==0) cout<<"NOT POSSIBLE";
else if(cnt>1) cout<<"NOT UNIQUE";
else cout<<((idx<=mid)? s.substr(mid+1):s.substr(1,n/2));//判断删掉的位置小于或大于中点来判断取左半边还是右半边
return 0;
}-P10479 匹配统计-
思路
- 字符串哈希之字符串哈希+二分模板
- 对字符串 和 分别预处理哈希值,枚举 的每个后缀起点 ,二分其最长匹配长度,并将答案统计到桶数组中,询问时直接输出桶内值即可
- 整体复杂度
Code
#include<bits/stdc++.h>
#define int long long
#define ull unsigned long long
#define pii pair<int,int>
using namespace std;
const int N=1e6+10,maxl=1e3+10,inf=0x3f3f3f3f3f3f,mod=1e9+7;
const int p=131;
int n,m,q;
int ans[N];
ull hs1[N],hs2[N],pw[N];
string s,t;
ull get_hash1(int l,int r)
{
return hs1[r]-hs1[l-1]*pw[r-l+1];
}
ull get_hash2(int l,int r)
{
return hs2[r]-hs2[l-1]*pw[r-l+1];
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin>>n>>m>>q;
cin>>s>>t;
s='@'+s;
t='@'+t;
pw[0]=1;
for(int i=1;i<=n;i++) hs1[i]=hs1[i-1]*p+s[i];
for(int i=1;i<=m;i++) hs2[i]=hs2[i-1]*p+t[i];
pw[0]=1ull;
for(int i=1;i<=(int)2e5;i++) pw[i]=pw[i-1]*p;
for(int i=1;i<=n;i++)
{
int l=-1,r=min(m,n-i+1)+1;
while(l+1<r)
{
int mid=(l+r)/2;
ull tmp1=get_hash1(i,i+mid-1),tmp2=get_hash2(1,mid);
if(tmp1==tmp2) l=mid;
else r=mid;
}
if(l==-1) ans[0]++;
else ans[l]++;
}
while(q--)
{
int x;
cin>>x;
cout<<ans[x]<<"\n";
}
return 0;
}-U577114 M形字符串-
思路
- 字符串哈希之回文哈希模板
- 一个字符串是 形字符串,当且仅当:
- 整个字符串本身是回文
- 字符串的前半段也是回文
- 所以我们可以在处理哈希的时候把这个字符串倒着也存一遍哈希值,这样就可以用倒过来的哈希值函数来判断字符串是否回文
- 若整个字符串与字符串的前半段均回文,则记录答案
Code
#include<bits/stdc++.h>
#define int unsigned long long
#define pii pair<int,int>
using namespace std;
const int N=1e6+10,maxl=1e3+10,inf=0x3f3f3f3f3f3f,mod=1e9+7;
const int p=131;
int n,ans;
int hs1[N],hs2[N],pw[N];
string s;
int get_hash(int l,int r)
{
return hs1[r]-hs1[l-1]*pw[r-l+1];
}
int hash_get(int l,int r)
{
return hs2[l]-hs2[r+1]*pw[r-l+1];
}
bool check(int l,int r)
{
return (get_hash(l,r)==hash_get(l,r));
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin>>s;
int len=s.size();
s='@'+s;
pw[0]=1;
for(int i=1;i<=len;i++) hs1[i]=hs1[i-1]*p+s[i];
for(int i=len;i>=1;i--) hs2[i]=hs2[i+1]*p+s[i];
for(int i=1;i<=len;i++) pw[i]=pw[i-1]*p;
for(int i=1;i<=len;i++)
{
if(check(1,i) && check(1,(i+1)/2)) ans++;
}
cout<<ans;
return 0;
}-P3805 【模板】Manacher(24pts)-
思路
回文子串分两类,分别枚举所有可能的中心:
- 奇数长度回文:中心是单个字符,共 个中心
- 偶数长度回文:中心在两个字符之间的缝隙,共 个中心
对每个中心,我们向外扩展找到以它为中心的最长回文半径,再计算总长度更新答案
优化 :预处理字符串哈希
预处理两个前缀哈希数组:
- 正序前缀哈希,用于计算任意子串正序的哈希值
- 逆序前缀哈希,用于计算任意子串逆序的哈希值
若一个子串正序哈希值=逆序哈希值,说明这个子串回文
优化 :二分查找
回文扩展满足单调性:
如果向外扩展 个字符仍然是回文,那么所有小于 的扩展长度也一定是回文
因此就不用一步步扩展,直接对每个中心二分查找最大半径:
- 奇数回文:判断 与 是否对称,二分得到最大半径 ,回文长度
- 偶数回文:判断 与 是否对称,二分得到最大半径 ,回文长度
最后由 取最大值即为答案
代码理论复杂度 ,但是空间和常数过大,只能拿到部分
Code
#include<bits/stdc++.h>
#define int unsigned long long
#define pii pair<int,int>
using namespace std;
const int N=2e7+10,maxl=1e3+10,inf=0x3f3f3f3f3f3f,mod=1e9+7;
const int p=131;
int n;
int ans=1;
int hs1[N],hs2[N],pw[N];
string s;
int get_hash(int l,int r)
{
return hs1[r]-hs1[l-1]*pw[r-l+1];
}
int hash_get(int l,int r)
{
return hs2[l]-hs2[r+1]*pw[r-l+1];
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin>>s;
int n=s.size();
s='@'+s;
pw[0]=1;
for(int i=1;i<=n;i++) hs1[i]=hs1[i-1]*p+s[i];
for(int i=n;i>=1;i--) hs2[i]=hs2[i+1]*p+s[i];
for(int i=1;i<=n;i++) pw[i]=pw[i-1]*p;
for(int i=1;i<=n-1;i++)
{
int l=0,r=min(i-1,n-i)+1;
while(l+1<r)
{
int mid=(l+r)/2;
if(get_hash(i-mid,i-1)==hash_get(i+1,i+mid)) l=mid;
else r=mid;
}
ans=max(ans,l*2+1);
l=0,r=min(i,n-i)+1;
while(l+1<r)
{
int mid=(l+r)/2;
if(get_hash(i-mid+1,i)==hash_get(i+1,i+mid)) l=mid;
else r=mid;
}
ans=max(ans,l*2);
}
cout<<ans;
return 0;
}-P6456 [COCI 2006/2007 #5] DVAPUT-
思路
单调性
如果存在长度为 的重复子串,那么所有长度小于 的子串一定也存在重复,满足单调性,因此可以用二分查找快速定位最大合法长度
int get_hash(int l,int r)
{
return hs[r]-hs[l-1]*pw[r-l+1];
}功能:计算任意子串的哈希值
bool check(int x)
{
mp.clear();
for(int i=1;i+x-1<=n;i++)
{
int tmp=get_hash(i,i+x-1);
mp[tmp]++;
if(mp[tmp]>=2) return true;
}
return false;
}功能:判断是否存在长度为 的子串在字符串中出现至少两次
执行流程:
- 清空哈希统计容器
- 遍历所有长度为 的子串,计算每个子串的哈希值
- 用 统计每个哈希值的出现次数
注: 普通 存入也需要时间,所以这里使用 - 只要有一个哈希值出现次数 ,说明该长度可行,可以尝试更长并更新左边界 ,否则更新右边界
最后左边界 即为最长重复子串的长度
Code
#include<bits/stdc++.h>
#define int unsigned long long
#define pii pair<int,int>
using namespace std;
const int N=1e6+10,maxl=1e3+10,inf=0x3f3f3f3f3f3f,mod=1e9+7;
const int p=131;
int n;
int hs[N],pw[N];
string s;
unordered_map<int,int> mp;
int get_hash(int l,int r)
{
return hs[r]-hs[l-1]*pw[r-l+1];
}
bool check(int x)
{
mp.clear();
for(int i=1;i+x-1<=n;i++)
{
int tmp=get_hash(i,i+x-1);
mp[tmp]++;
if(mp[tmp]>=2) return true;
}
return false;
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>s;
s='@'+s;
pw[0]=1;
for(int i=1;i<=n;i++)
{
hs[i]=hs[i-1]*p+s[i];
pw[i]=pw[i-1]*p;
}
int l=0,r=n;
while(l+1<r)
{
int mid=(l+r)/2;
if(check(mid)) l=mid;
else r=mid;
}
cout<<l;
return 0;
}-P3498 [POI 2010] KOR-Beads-
思路
因为一段项链和它的反转是同一种,所以对每一段同时计算正序与逆序哈希值,两个哈希值对应同一种项链:
- 只要其中一个哈希值已被记录,就说明该项链已经统计过,不重复计数
- 遇到新项链时,同时将两个哈希值标记为已出现,保证后续遇到正或逆序都能进行去重
外层枚举每个段长 (即题目中的 ):
- 按步长 遍历所有完整段:
- 计算当前段的正序哈希 与逆序哈希
- 若两个哈希都未出现过,则说明是全新的项链,累加
- 将两个哈希都标记为已出现,完成正逆双向去重
- 将当前 加入 (表示有 种项链对应的 集合),同时更新全局最大值
剪枝
if(n/len<ans) break;对于长度 ,最多只能有 段,因此不同项链数的理论上界就是
当 已经小于当前最优解 时,更大的 只会让 更小,永远不可能超越当前最大值,因此直接终止外层枚举
最后按题目要求输出即可
Code
#include<bits/stdc++.h>
#define int unsigned long long
#define pii pair<int,int>
using namespace std;
const int N=1e6+10,maxl=1e3+10,inf=0x3f3f3f3f3f3f,mod=1e9+7;
const int p=20140131;
int n;
int a[N],hs1[N],hs2[N],pw[N];
map<int,int> mp;
vector<int> k[N];
int get_hash(int l,int r)
{
return hs1[r]-hs1[l-1]*pw[r-l+1];
}
int hash_get(int l,int r)
{
return hs2[l]-hs2[r+1]*pw[r-l+1];
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
pw[0]=1;
for(int i=1;i<=n;i++)
{
hs1[i]=hs1[i-1]*p+a[i];
pw[i]=pw[i-1]*p;
}
for(int i=n;i>=1;i--) hs2[i]=hs2[i+1]*p+a[i];
int ans=0;
for(int len=1;len<=n;len++)
{
if(n/len<ans) break;
int cnt=0;
mp.clear();
for(int i=1;i+len-1<=n;i+=len)
{
int tmp1=get_hash(i,i+len-1),tmp2=hash_get(i,i+len-1);
if(!mp[tmp1] && !mp[tmp2]) cnt++;
mp[tmp1]=1;
mp[tmp2]=1;
}
k[cnt].push_back(len);
ans=max(ans,cnt);
}
cout<<ans<<" "<<k[ans].size()<<"\n";
for(int x:k[ans]) cout<<x<<" ";
return 0;
}-AT_abc367_f [ABC367F] Rearrange Query-
思路
两个子串可以通过重排完全相等,等价于两个子串的元素多重集合完全相同
所以问题转化为:每次查询判断 的区间 与 的区间 是否为同一多重集合
如果两个多重集合完全相同,那么它们的 次幂和, 次幂和, 次幂和, 次幂和必然全部相等
反之,当足够多阶的幂和都相等时,两个多重集合不相同的概率极低,也可视为相等
理解完问题后,求和部分我们就可以用前缀和来求
定义
- 数组 前 个元素的 次方之和
- 数组 前 个元素的 次方之和
递推公式
其中幂次部分可以用快速幂来求(其实暴力也行)
判断流程
- 若 ,两个区间长度不同,重排后不可能相等,直接输出
- 依次校验 阶的区间幂和:
- 只要有任意一阶幂和不相等,说明多重集合不同,标记为 并跳出
- 若全部 阶幂和都相等,判定两个区间多重集合相同,可以通过重排得到,输出
Code
#include<bits/stdc++.h>
#define int unsigned long long
#define pii pair<int,int>
using namespace std;
const int N=1e6+10,maxl=1e3+10,inf=0x3f3f3f3f3f3f,mod=1e9+7;
int n,q;
int a[N],b[N];
int pre1[N][30],pre2[N][30];
int quick_pow(int a,int b)
{
int res=1ull;
while(b)
{
if(b&1) res*=a;
a*=a;
b>>=1;
}
return res;
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin>>n>>q;
for(int i=1;i<=n;i++)
{
cin>>a[i];
for(int j=1;j<=20;j++) pre1[i][j]=pre1[i-1][j]+quick_pow(a[i],j);
}
for(int i=1;i<=n;i++)
{
cin>>b[i];
for(int j=1;j<=20;j++) pre2[i][j]=pre2[i-1][j]+quick_pow(b[i],j);
}
while(q--)
{
int l1,r1,l2,r2;
cin>>l1>>r1>>l2>>r2;
if(r1-l1!=r2-l2)
{
cout<<"No\n";
continue;
}
bool flag=true;
for(int i=1;i<=20;i++)
{
if((pre1[r1][i]-pre1[l1-1][i])!=(pre2[r2][i]-pre2[l2-1][i]))
{
flag=false;
break;
}
}
cout<<((flag)? "Yes":"No")<<"\n";
}
return 0;
}-P8023 [ONTAK2015] Tasowanie-
思路
核心策略
其实就是贪心
用双指针 分别指向 的当前待选位置,逐位选择:
- 当前元素不同:直接输出数值更小的那个,保证当前位字典序最优,对应指针后移
- 当前元素相同:则不能随意选,必须比较从当前位置开始的整个剩余后缀的字典序,输出后缀更小的那一边的首元素,只有后缀整体更小,才能保证合并后的全局字典序最小
优化
如果暴力逐位比较后缀,最坏时间复杂度为 ,无法通过 的数据,所以我们可以用前缀哈希+二分查找将单次后缀比较优化到 ( 为剩余长度):
二分求最长公共前缀
当 时,二分查找最大的长度 ,使得 从 开始的 个元素,与 从 开始的 个元素完全相等
- 二分边界:左边界 ,右边界为两边剩余长度的较小值
- 判定方式:比较对应子串的哈希值,相等则说明前缀匹配
后缀大小判定
得到最长公共前缀 后:
- 若两边都还有剩余元素,则比较第 位,哪一边更小,对应后缀就更小,选择该边的当前元素
- 若其中一边已经没有剩余元素,说明该边后缀更短(字典序更小),但由于当前元素相等,选任意一边输出的数值都相同,不影响字典序,后续继续归并即可
Code
#include<bits/stdc++.h>
#define int long long
#define pii pair<int,int>
using namespace std;
const int N=2e5+10,maxl=1e3+10,inf=0x3f3f3f3f3f3f,mod=1e9+7;
const int p=131;
int n,m;
int a[N],b[N],hs1[N],hs2[N],pw[N];
int get_hash1(int l,int r)
{
return hs1[r]-hs1[l-1]*pw[r-l+1];
}
int get_hash2(int l,int r)
{
return hs2[r]-hs2[l-1]*pw[r-l+1];
}
signed main()
{
ios::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin>>n;
for(int i=1;i<=n;i++) cin>>a[i];
cin>>m;
for(int i=1;i<=m;i++) cin>>b[i];
pw[0]=1;
for(int i=1;i<=(int)4e5;i++) pw[i]=pw[i-1]*p;
for(int i=1;i<=n;i++) hs1[i]=hs1[i-1]*p+a[i];
for(int i=1;i<=m;i++) hs2[i]=hs2[i-1]*p+b[i];
int x=1,y=1;
while(x<=n && y<=m)
{
if(a[x]!=b[y]) cout<<((a[x]<b[y])? a[x++]:b[y++]);
else
{
int l=0,r=min(n-x+1,m-y+1)+1;
while(l+1<r)
{
int mid=(l+r)/2;
if(get_hash1(x,x+mid-1)==get_hash2(y,y+mid-1)) l=mid;
else r=mid;
}
int res=l;
if(x+res-1==n) cout<<b[y++];
else if(y+res-1==m) cout<<a[x++];
else cout<<((a[x+res]<b[y+res])? a[x++]:b[y++]);
}
cout<<" ";
}
while(x<=n) cout<<a[x++]<<" ";
while(y<=m) cout<<b[y++]<<" ";
return 0;
}