bzoj 3752: Hack 预处理+暴力dfs
题目大意:
定义字符串的hash值\(h = \sum_{i=0}^{n-1}p^{n-i-1}s_i\)
现在给定K个长度不超过L的字符串S,对于每个字符串S,求字典序最小长度不超过L的字符串T使得T不同于S但是Hash值相同.\(P\leq 2^{31},L \leq 8,K \leq 15\)
题解:
我们考虑直接爆搜\(O(26^L)\)
肯定过不了啊。。。
但是我们发现,如果我们枚举前L-1位,那么最后一位可以直接计算出来.
所以可以做到\(O(26^{L-1})\)
我们再进一步,如果我们枚举前L-4位,后4位我们打表预处理出来。
可以做到\(O(26^(L-4)log26^4)\)
我们发现这个复杂度妥妥地能过。
但是嘛。。。也许是我写丑了。。。本地跑的特别慢但是bzoj能A...
这道题就是个大毒瘤,写了一下午!
#include <map>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
typedef long long ll;
typedef unsigned int uint;
inline void read(uint &x){
x=0;char ch;bool flag = false;
while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
const int maxn = 22;
uint P,L,K,n;
char ss[26*26*26*26 + 26*26*26 + 26*26 + 26 + 10][6];
uint cnt = 0;
inline uint qpow(uint x,uint p){
uint ret = 1;
for(;p;x=x*x,p>>=1) if(p&1) ret=ret*x;
return ret;
}
char s[maxn];
inline uint hash(char *s,uint n){
uint ret = 0;
for(uint i=0;i<n;++i){
ret += (uint)s[i]*qpow(P,n-i-1);
}
return ret;
}
uint val;bool flag;
char t[maxn];uint len;
char ans[maxn];
inline void judge(uint m){
bool cp = false;
if(n != m) cp = true;
else for(uint i=0;i<n;++i) if(s[i] != t[i]) cp = true;
if(!cp) return;
if(hash(t,m) == val){
if(!flag){
flag = true;len = m;
for(uint i = 0;i<len;++i){
ans[i] = t[i];
}
}else{
bool sw = false;
for(uint i = 0;i<len && i < m;++i){
if(t[i] < ans[i]){
sw = true;
break;
}
}
if(sw || (!sw && m < len)){
for(uint i=0;i<m;++i){
ans[i] = t[i];
}len = m;
}
}
}
}
inline void judge(char* t){
int m = strlen(t);
bool cp = false;
if(n != m) cp = true;
else for(uint i=0;i<n;++i) if(s[i] != t[i]) cp = true;
if(!cp) return;
if(hash(t,m) == val){
if(!flag){
flag = true;len = m;
for(uint i = 0;i<len;++i){
ans[i] = t[i];
}
}else{
bool sw = false;
for(uint i = 0;i<len && i < m;++i){
if(t[i] < ans[i]){
sw = true;
break;
}
}
if(sw || (!sw && m < len)){
for(uint i=0;i<m;++i){
ans[i] = t[i];
}len = m;
}
}
}
}
inline void calc(uint m){
uint x = hash(t,m);
x = x*P;
uint y = val - x;
t[m] = y;
if(y >= 'A' && y <= 'Z')judge(m+1);
}
void dfs(uint pos){
if(pos == L-1){
calc(pos);
return;
}
for(char ch='A';ch<='Z';++ch){
t[pos] = ch;
dfs(pos+1);
if(flag) return;
judge(pos);
if(flag) return;
}
}
char tmp[6];
map<int,int>mp2,mp3,mp4;
char query[maxn][maxn];
inline void save4(){
uint h = hash(tmp,4);
if(mp4[h] == 0){
for(uint i=1;i<=K;++i){
if(!strcmp(tmp,query[i]))
return;
}
++cnt;
for(uint i=0;i<4;++i){
ss[cnt][i] = tmp[i];
}
mp4[h] = cnt;
}
}
inline void save3(){
uint h = hash(tmp,3);
if(mp3[h] == 0){
for(uint i=1;i<=K;++i){
if(!strcmp(tmp,query[i]))
return;
}
++cnt;
for(uint i=0;i<3;++i){
ss[cnt][i] = tmp[i];
}
mp3[h] = cnt;
}
}
inline void save2(){
uint h = hash(tmp,2);
if(mp2[h] == 0){
for(uint i=1;i<=K;++i){
if(!strcmp(tmp,query[i]))
return;
}
++cnt;
for(uint i=0;i<2;++i){
ss[cnt][i] = tmp[i];
}
mp2[h] = cnt;
}
}
inline void dfss(uint pos){
if(pos == 2) save2();
if(pos == 3) save3();
if(pos == 4){
save4();
return ;
}
for(char ch = 'A';ch<='Z';++ch){
tmp[pos] = ch;
dfss(pos+1);
}
}
inline void ca4(uint m){
uint x = hash(t,m);
x = x*P*P*P*P;
uint y = val - x;
for(int i=1;i<=K;++i){
if(strlen(query[i]) == 4){
for(int j=0;j<4;++j){
t[m+j] = query[i][j];
}
judge(m+4);
}
}
if(mp4[y] == 0) return;
for(int i=0;i<4;++i){
t[m+i] = ss[mp4[y]][i];
}
judge(m+4);
}
inline void ca3(uint m){
uint x = hash(t,m);
x = x*P*P*P;
uint y = val - x;
for(int i=1;i<=K;++i){
if(strlen(query[i]) == 3){
for(int j=0;j<3;++j){
t[m+j] = query[i][j];
}
judge(m+3);
}
}
if(mp3[y] == 0) return;
for(int i=0;i<3;++i){
t[m+i] = ss[mp3[y]][i];
}
judge(m+3);
}
inline void ca2(uint m){
uint x = hash(t,m);
x = x*P*P;
uint y = val - x;
for(int i=1;i<=K;++i){
if(strlen(query[i]) == 3){
for(int j=0;j<3;++j){
t[m+j] = query[i][j];
}
judge(m+3);
}
}
if(mp2[y] == 0) return;
for(int i=0;i<2;++i){
t[m+i] = ss[mp2[y]][i];
}
judge(m+2);
}
void search(uint pos){
if(pos == L-4){
calc(pos);
ca2(pos);
ca3(pos);
ca4(pos);
return;
}
for(char ch='A';ch<='Z';++ch){
t[pos] = ch;
search(pos+1);
if(flag) return;
judge(pos);
if(flag) return;
}
calc(pos);ca2(pos);ca3(pos);ca4(pos);
}
int main(){
read(P);read(L);read(K);
for(uint i=1;i<=K;++i) scanf("%s",query[i]);
dfss(0);
for(uint i=1;i<=K;++i){
n = strlen(query[i]);
for(uint j=0;j<n;++j) s[j] = query[i][j];
val = hash(s,n);
flag = false;
if(L <= 4) dfs(0);
else search(0);
if(flag) ans[len] = 0,printf("%s\n",ans);
else puts("-1");
}
getchar();getchar();
return 0;
}
bzoj 3752: Hack 预处理+暴力dfs的更多相关文章
- hihoCoder 1185 连通性·三(Tarjan缩点+暴力DFS)
#1185 : 连通性·三 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 暑假到了!!小Hi和小Ho为了体验生活,来到了住在大草原的约翰家.今天一大早,约翰因为有事要出 ...
- Strange Country II 暴力dfs
这题点的个数(<=50)有限, 所以可以纯暴力DFS去搜索 //#pragma comment(linker, "/STACK:16777216") //for c++ Co ...
- UVA129 暴力dfs,有许多值得学习的代码
紫书195 题目大意:给一个困难的串,困难的串的定义就是里面没有重复的串. 思路:不需要重新对之前的串进行判重,只需要对当前的加入的字符进行改变即可. 因为是判断字典序第k个的字符串,所以要多一个全局 ...
- 2018杭电多校第五场1002(暴力DFS【数位】,剪枝)
//never use translation#include<bits/stdc++.h>using namespace std;int k;char a[20];//储存每个数的数值i ...
- A. The Fault in Our Cubes 暴力dfs
http://codeforces.com/gym/101257/problem/A 把它固定在(0,0, 0)到(2, 2, 2)上,每次都暴力dfs检查,不会超时的,因为规定在这个空间上,一不行, ...
- 【BZOJ】1675: [Usaco2005 Feb]Rigging the Bovine Election 竞选划区(暴力dfs+set判重)
http://www.lydsy.com/JudgeOnline/problem.php?id=1675 一开始我写了个枚举7个点....... 但是貌似... 写挫了. 然后我就写dfs.. 判重好 ...
- bzoj 4765 普通计算姬 dfs序 + 分块
题目链接 Description "奋战三星期,造台计算机".小G响应号召,花了三小时造了台普通计算姬.普通计算姬比普通计算机要厉害一些.普通计算机能计算数列区间和,而普通计算姬能 ...
- Codeforces Round #359 (Div. 2) C. Robbers' watch (暴力DFS)
题目链接:http://codeforces.com/problemset/problem/686/C 给你n和m,问你有多少对(a, b) 满足0<=a <n 且 0 <=b &l ...
- UVA 185(暴力DFS)
Roman Numerals The original system of writing numbers used by the early Romans was simple but cum ...
随机推荐
- table中tr的display属性在火狐中显示不正常,IE中显示正常
最近在作项目的时候碰到一个问题,就是需要AJAX来交互显示<tr> </tr> 标签内的东西,按照常理,对于某一单元行需要显示时,使用:display:block属性,不需要显 ...
- 【BZOJ4927】第一题 双指针+DP(容斥?)
[BZOJ4927]第一题 Description 给定n根直的木棍,要从中选出6根木棍,满足:能用这6根木棍拼 出一个正方形.注意木棍不能弯折.问方案数. 正方形:四条边都相等.四个角都是直角的四边 ...
- 1.Python学习---helloworld
1.首先访问http://www.python.org/download/去下载最新的python版本. 2.安装下载包,一路next. 3.为计算机添加安装目录搭到环境变量,如图把python的安装 ...
- [Oracle]根据字段值全库搜索相关数据表和字段
这个需求比较冷门,但对于在某些特定的情况下,还是会有这样的需要的.好在Oracle实现还比较方便,用存储过程则轻松实现. 查询字符串: create or replace procedure sear ...
- springboot带分页的条件查询
QueryDSL简介 QueryDSL仅仅是一个通用的查询框架,专注于通过Java API构建类型安全的SQL查询. Querydsl可以通过一组通用的查询API为用户构建出适合不同类型ORM框架或者 ...
- Js中的apply和call
1.call和apply都是为了改变某个函数运行时的上下文而存在的 2.也就是改变函数体内this的指向. 3.二者的作用完全一样,只是接受参数的方式不太一样. 4.call 需要把参数按顺序传递进去 ...
- ip地址设备信息
其实是个小问题,在项目中遇到要获取手机ip地址和mac地址的问题,mac地址获取用appcan的uexDevice插件可以实现. 但是ip地址,获取的方式很多,具体有搜狐/腾讯/新浪等提供的接口.但是 ...
- 33_为应用添加多个Activity与参数传递
1\ 2\ 3\ 4\ 2 3
- 20165101刘天野 2018-2019-2《网络对抗技术》Exp6 信息搜集与漏洞扫描
目录 20165101刘天野 2018-2019-2<网络对抗技术>Exp6 信息搜集与漏洞扫描 1.实验内容 1.1 各种搜索技巧的应用 1.2 DNS IP注册信息的查询 1.3 基本 ...
- java入门了解12
1.SequenceInputStream序列流:能将其他输入流的串联 用处:读完第一个再去读第二个输入流 用法:构造方法:SequenceInputStream(InputStream s1,Inp ...