传送门
题意: 给一个二进制串aaa,让后定义两个串相似为有至少一个相同位置相等。现在让你找一个字典序最小的长度为kkk的串使其与aaa中每个长度为kkk的字串相似。
思路: 首先我们知道所有可能的串一共有2k2^k2k,我们把aaa串全部取反,这样aaa的所有长度为kkk的串都是不符合条件的,除去这n−k+1n-k+1n−k+1个串,其他的串都是符合条件的,那么我们从000开始枚举到n−k+1n-k+1n−k+1转换成二进制一定可以找到一个符合条件的串。但是我们不一定有n−k+1n-k+1n−k+1个,上限为2k2^{k}2k个情况,二者取min即可。如果到最后都没找到那说明不存在。
二进制串的话整个哈希就好了,能很快的得到hash值,白嫖lc哥的板子。
#include<cstdio>
#include<iostream>
#include<string>
#include<cstring>
#include<map>
#include<cmath>
#include<cctype>
#include<vector>
#include<set>
#include<queue>
#include<algorithm>
#include<sstream>
#include<ctime>
#include<cstdlib>
#define X first
#define Y second
#define L (u<<1)
#define R (u<<1|1)
#define pb push_back
#define mk make_pair
#define Mid (tr[u].l+tr[u].r>>1)
#define Len(u) (tr[u].r-tr[u].l+1)
#define random(a,b) ((a)+rand()%((b)-(a)+1))
#define db puts("---")
using namespace std
;
typedef long long LL
;
typedef unsigned long long ULL
;
typedef pair
<LL
,LL
> PII
;const int N
=1000010,mod
=1e9+7,INF
=0x3f3f3f3f;
const double eps
=1e-6;int k
,n
;
int a
[N
],ans
,tot
;
LL now1
,now2
;
char ss
[N
];
map
<LL
,int>mp1
,mp2
;struct Hash
{char s
[N
];LL hash
[N
][2],base
[2],mod
[2],f
[N
][2],ban
;int n
;void init(int id
){ban
=id
;base
[0]=233333;base
[1]=23333333;mod
[0]=1e9+7;mod
[1]=1e9+9;for(int k
=0;k
<2;k
++){hash
[0][k
]=0;f
[0][k
]=1;for(int i
=1;i
<=n
;i
++){f
[i
][k
]=f
[i
-1][k
]*base
[k
]%mod
[k
];hash
[i
][k
]=(hash
[i
-1][k
]*base
[k
]+s
[i
])%mod
[k
];}}}pair
<LL
,LL
>get_hash(int l
,int r
){if(l
<=0||r
>n
) return make_pair(ban
,ban
);return make_pair((hash
[r
][0]-hash
[l
-1][0]*f
[r
-l
+1][0]%mod
[0]+mod
[0])%mod
[0],(hash
[r
][1]-hash
[l
-1][1]*f
[r
-l
+1][1]%mod
[1]+mod
[1])%mod
[1]);}
}s
,t
;void get(int ans
)
{string ss
;while(ans
) ss
+=ans
%2+'0',ans
/=2;while(ss
.size()<k
) ss
+='0';now1
=now2
=0;for(int i
=ss
.length()-1;i
>=0;i
--) now1
=(now1
*s
.base
[0]+ss
[i
])%s
.mod
[0],now2
=(now2
*s
.base
[1]+ss
[i
])%s
.mod
[1];
}bool check()
{for(int i
=0;i
<=min(n
-k
+1,(1<<(min(26,k
)))-1);i
++){get(i
);if(mp1
[now1
]||mp2
[now2
]) continue;ans
=i
;return true;}return false;
}string
solve()
{string s
;while(ans
) s
+=ans
%2+'0',ans
/=2;while(s
.size()<k
) s
+='0';reverse(s
.begin(),s
.end());return s
;
}int main()
{
int _
; scanf("%d",&_
);while(_
--){scanf("%d%d",&n
,&k
); scanf("%s",s
.s
+1); s
.n
=n
;for(int i
=1;i
<=n
;i
++) s
.s
[i
]=((s
.s
[i
]-'0')^1)+'0';s
.init(1); mp1
.clear(); mp2
.clear();for(int i
=k
;i
<=n
;i
++){PII t
=s
.get_hash(i
-k
+1,i
);mp1
[t
.X
]=1; mp2
[t
.Y
]=1;}if(check()) printf("YES\n%s\n",solve().c_str());else printf("NO\n");}return 0;
}
总结
以上是生活随笔为你收集整理的Educational Codeforces Round 101 (Rated for Div. 2) F. Power Sockets 哈希 + 乱搞的全部内容,希望文章能够帮你解决所遇到的问题。
如果觉得生活随笔网站内容还不错,欢迎将生活随笔推荐给好友。