咯咯咯
DVAPUT
题意+思路
给定一个字符串,求至少出现两次的子串的长度最长是多长
我们可以用二分答案来求长度,遍历所有该长度的子串,如果出现次数大于1,则说明该长度可行,将范围增大;否则将范围缩小。
但是统计子串的时间复杂度太高了,我们不难想到可以用哈希来计算子串的哈希值,再统计该哈希值出现的次数即可。
注意!因为哈希值可能很多,我们要用unordered_map来存储
一!定!要!清!空!!!
最后输出l即可
丑陋代码
#include<bits/stdc++.h>
#define endl '\n'
#define int unsigned long long
using namespace std;
int hs[200005],pw[200005],n;
unordered_map<int,int>mp;
string s;
int hs1(int l,int r)
{
return hs[r]-hs[l-1]*pw[r-l+1];
}
int check(int t)
{
mp.clear();
for(int i=1;i<=n-t+1;i++)
{
int has=hs1(i,i+t-1);
mp[has]++;
if(mp[has]>1)return 1;
}
return 0;
}
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]*1145+s[i];
pw[i]=pw[i-1]*1145;
}
signed l=0,r=n+1;
while(l+1<r)
{
int mid=(l+r)/2;
if(check(mid))l=mid;
else r=mid;
}
cout<<l;
return 0;
}KOR-Beads
题意+思路
给定n个数,我们可以选定一个k,使得组成(末尾不要)串项链中,不同的项链数量最多
不同指的是翻转后仍然不同(如(1,2,3)与(3,2,1)是一样的)因为它需要翻转后仍然不同,所以我们需要两个哈希值(翻转前和后),如果它们都不同,才说明这两串项链是不同的。
我们可以枚举长度,求出当前长度的不同项链的数量
用一个vector二维数组,把不同项链数量为i的长度都push_back进ans[i]里,并记录当前长度的不同项链的数量的最大值输出最大值,不同项链的数量等于最大值的k的个数和不同项链的数量等于最大值的所有k
丑陋代码
#include<bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
int hsq[200005],hsh[200005],pw[200005],n,k=1,a[200005];
map<int,int>mp;
vector<int>ans[200005];
int hs1(int l,int r)
{
return hsq[r]-hsq[l-1]*pw[r-l+1];
}
int hs2(int l,int r)
{
return hsh[l]-hsh[r+1]*pw[r-l+1];
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n;
pw[0]=1;
for(int i=1;i<=n;i++)
{
cin>>a[i];
hsq[i]=hsq[i-1]*19260817+a[i];
pw[i]=pw[i-1]*19260817;
}
for(int i=n;i>=1;i--)hsh[i]=hsh[i+1]*19260817+a[i];
for(int i=1;i<=n/k;i++)
{
int s=0;
mp.clear();
for(int j=1;j<=n-i+1;j+=i)
{
if(mp[hs1(j,j+i-1)]==0&&mp[hs2(j,j+i-1)]==0)s++;
mp[hs1(j,j+i-1)]=1;
mp[hs2(j,j+i-1)]=1;
}
k=max(s,k);
ans[s].push_back(i);
}
cout<<k<<" "<<ans[k].size()<<endl;
for(int i=0;i<ans[k].size();i++)cout<<ans[k][i]<<" ";
return 0;
}AT_abc367_f
题意+思路
给定长度为n的序列a,b和q次查询,每次查询给定两个区间(l,r)和(l1,r1),求区间(l,r)是否能排列成区间(l1,r1)
这道题原本是要同时用前缀和与开方和(可能还有立方和)才能过的,但我们的
神奇芒果老师居然连(1,1,3)=5,(1,2,2)=5的卡前缀和样例都没想到,直接让前缀和水灵灵的AC了
耶!!!输入a,b序列并求出它们各自的前缀和,对于每次查询,直接求出区间和并比较即可。
丑陋代码
#include<bits/stdc++.h>
#define endl '\n'
#define int long long
using namespace std;
int hsa[200005],hsb[200005],n,q;
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>q;
int x;
for(int i=1;i<=n;i++)
{
cin>>x;
hsa[i]=hsa[i-1]+x;
}
for(int i=1;i<=n;i++)
{
cin>>x;
hsb[i]=hsb[i-1]+x;
}
while(q--)
{
int l,r,l1,r1;
cin>>l>>r>>l1>>r1;
if(r-l!=r1-l1||hsa[r]-hsa[l-1]!=hsb[r1]-hsb[l1-1])cout<<"No"<<endl;
else cout<<"Yes"<<endl;
}
return 0;
}Tasowanie
题意+思路
给定两个数字串a,b,求将a和b进行二路归并后可得到的字典序最小的数字串
如果a的第一个字母小于 b的第一个字母就选a的;如果a的第一个字母大于 b的第一个字母就选b的
如果它们相等的话,那么我们需要用二分来求出数字串a与数字串b里剩下的第一个不相等的位置,再进行大小比较,输出小的那个数字串的第一个字母。
如果第一个不相等的位置在一个数字串的末尾,则说明该字符串是另一个字符串的前缀,输出另一个字符串的第一个字母。
丑陋代码
#include<bits/stdc++.h>
#define endl '\n'
#define int unsigned long long
using namespace std;
int n,m,a[200005],b[200005],hsa[200005],hsb[200005],pw[200005];
int hs1(int l,int r)
{
return hsa[r]-hsa[l-1]*pw[r-l+1];
}
int hs2(int l,int r)
{
return hsb[r]-hsb[l-1]*pw[r-l+1];
}
signed main()
{
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
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<=n;i++)hsa[i]=hsa[i-1]*1145+a[i];
for(int i=1;i<=m;i++)hsb[i]=hsb[i-1]*1145+b[i];
for(int i=1;i<=2e5;i++)pw[i]=pw[i-1]*1145;
int x=1,y=1;
while(x<=n&&y<=m)
{
if(a[x]>b[y])
{
cout<<b[y++]<<" ";
}
else if(a[x]<b[y])
{
cout<<a[x++]<<" ";
}
else
{
int l=0;
int r=min(n-x+1,m-y+1)+1;
while(l+1<r)
{
int mid=(l+r)/2;
if(hs1(x,x+mid-1)==hs2(y,y+mid-1))l=mid;
else r=mid;
}
if(x+l-1==n)
{
cout<<b[y++]<<" ";
}
else if(y+l-1==m)
{
cout<<a[x++]<<" ";
}
else
{
if(a[x+l]<b[y+l])cout<<a[x++]<<" ";
else cout<<b[y++]<<" ";
}
}
}
while(x<=n)cout<<a[x++]<<" ";
while(y<=m)cout<<b[y++]<<" ";
return 0;
}