P6456 [COCI 2006/2007 #5] DVAPUT
枚举长度len,在枚举i找以i为左端点,长度为len的子串,统计有几个不同的子串,如果有重复的子串,统计答案,
使用map统计子串
时间复杂度:
L=2e5直接爆炸
我们可以使用二分枚举len,哈希比较子串
map卡常
还是过不了
使用unordered_map
code
不是,你还真看代码呀
#include<bits/stdc++.h>
using namespace std;
#define int unsigned long long
int n;
int p=131;
int pw[2000005];
int hs[2000005];
int l,r,mid;
bool q;
int o;
string s;
void csh(){
pw[0]=1;
for(int i=1;i<=n;i++){
pw[i]=pw[i-1]*p;
hs[i]=hs[i-1]*p+s[i];
}
}
int gh(int x,int y){
return hs[y]-hs[x-1]*pw[y-x+1];
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>s;
s="#"+s;
csh();
l=0;
r=n;
while(l+1<r){
unordered_map<int,int> mp;
mid=(l+r)/2;
q=1;
for(int i=1;i+mid+1<=n;i++){
if(mp[gh(i,i+mid-1)]==1){
q=0;
l=mid;
break;
}
mp[gh(i,i+mid-1)]=1;
}
if(q){
r=mid;
}
}
cout<<l;
}P3498 [POI 2010] KOR-Beads
题目让我们找,长度多少时,可以用最多不同的子串
并输出所有能使子串最多的长度
枚举长度len,在枚举i找以i为左端点,长度为len的子串,统计有几个不同的子串,正反都要比,将结果存在ans[len]中,并统计最大值,最后再找有几个ans[len]为最大值
使用map统计
使用unordered_map统计
还可以剪枝
…看起来很暴力的思路是正解
code
不是,你还真看代码呀
#include<bits/stdc++.h>
using namespace std;
#define int unsigned long long
int n,m;
int v[1000000];
int w[1000000];
int a[30][1000000];
int b[30][1000000];
int l1,l2,r1,r2;
int po(int x,int y){
int res=1;
for(int i=1;i<=y;i++){
res*=x;
}
return res;
}
bool ck(){
if(r1-l1!=r2-l2){
return 0;
}
for(int i=1;i<=20;i++){
if((a[i][r1]-a[i][l1-1])!=(b[i][r2]-b[i][l2-1])){
return 0;
}
}
return 1;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>v[i];
for(int j=1;j<=20;j++){
a[j][i]=a[j][i-1]+po(v[i],j);
}
}
for(int i=1;i<=n;i++){
cin>>w[i];
for(int j=1;j<=20;j++){
b[j][i]=b[j][i-1]+po(w[i],j);
}
}
for(int i=1;i<=m;i++){
cin>>l1>>r1>>l2>>r2;
if(ck()){
cout<<"Yes\n";
}else{
cout<<"No\n";
}
}
}U584883 AT_abc367_f
本题暴力
统计两个区间内每个数有多少
最后每个数进行比较数量,相同YES反之NO
我们可以使用前缀哈希
统计前缀和,如果前缀和相同则认为YES,否则NO
但万一前缀和相同,实际的数组不相同(也算作哈希碰撞)怎么办
处理哈希碰撞时,我们可以用双哈希,同理,前缀哈希也有类似的
hs[i][j]表示前j个数的i次方和
如果两个区间的所有次方和都相等,那么YES否则NO
…其实只要前缀和也能过,数据水完了
code
不是,你还真看代码呀
#include<bits/stdc++.h>
using namespace std;
#define int unsigned long long
int n,m;
int v[1000000];
int w[1000000];
int a[30][1000000];
int b[30][1000000];
int l1,l2,r1,r2;
int po(int x,int y){
int res=1;
for(int i=1;i<=y;i++){
res*=x;
}
return res;
}
bool ck(){
if(r1-l1!=r2-l2){
return 0;
}
for(int i=1;i<=20;i++){
if((a[i][r1]-a[i][l1-1])!=(b[i][r2]-b[i][l2-1])){
return 0;
}
}
return 1;
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
for(int i=1;i<=n;i++){
cin>>v[i];
for(int j=1;j<=20;j++){
a[j][i]=a[j][i-1]+po(v[i],j);
}
}
for(int i=1;i<=n;i++){
cin>>w[i];
for(int j=1;j<=20;j++){
b[j][i]=b[j][i-1]+po(w[i],j);
}
}
for(int i=1;i<=m;i++){
cin>>l1>>r1>>l2>>r2;
if(ck()){
cout<<"Yes\n";
}else{
cout<<"No\n";
}
}
}P8023 [ONTAK2015] Tasowanie
贪心:
每次输出小的
此时会出现一种情况,如果两个数相同,怎么输出,很明显那个后面的数更小输出那个
此时要思考一个问题
如果从头比到底,把一个字符串比完了都相等,那么输出哪一个
我们会输出没有被比完的字符串,因为没有被比完说明后面有无限可能,有可能会更小
如果不用任何优化
很明显如果l~ r都相等,那么l~ r-1也都相等,具有单调性,用二分找
哈希
能过了
code
不是,你还真看代码呀
#include<bits/stdc++.h>
using namespace std;
#define int unsigned long long
int n,m;
int v[300000];
int w[300000];
int p=131;
int hsa[300000];
int hsb[300000];
int pw[300000];
int u=1e9;
int x,y,l,r,mid;
int po,pe;
void csh(){
pw[0]=1;
for(int i=1;i<=n+m;i++){
pw[i]=pw[i-1]*p;
}
for(int i=1;i<=n;i++){
hsa[i]=hsa[i-1]*p+v[i];
}
for(int i=1;i<=m;i++){
hsb[i]=hsb[i-1]*p+w[i];
}
}
int gha(int x,int y){
return hsa[y]-hsa[x-1]*pw[y-x+1];
}
int ghb(int x,int y){
return hsb[y]-hsb[x-1]*pw[y-x+1];
}
signed main(){
cin>>n;
for(int i=1;i<=n;i++){
cin>>v[i];
}
cin>>m;
for(int i=1;i<=m+n;i++){
cin>>w[i];
}
csh();
po=1;
pe=1;
while(po<=n&&pe<=m){
if(v[po]<w[pe]){
cout<<v[po]<<" ";
po++;
}else if(v[po]>w[pe]){
cout<<w[pe]<<" ";
pe++;
}else{
l=0;
r=min(n-po+1,m-pe+1)+1;
while(l+1<r){
mid=(l+r)/2;
if(gha(po,po+mid-1)==ghb(pe,pe+mid-1)){
l=mid;
}else{
r=mid;
}
}
if(po+l-1==n){
cout<<w[pe]<<" ";
pe++;
}else if(pe+l-1==m){
cout<<v[po]<<" ";
po++;
}else{
if(v[po+l]<w[pe+l]){
cout<<v[po]<<" ";
po++;
}else if(v[po+l]>w[pe+l]){
cout<<w[pe]<<" ";
pe++;
}
}
}
}
while(po<=n){
cout<<v[po]<<" ";
po++;
}
while(pe<=m){
cout<<w[pe]<<" ";
pe++;
}
}
