题目大意:

定义字符串的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的更多相关文章

  1. hihoCoder 1185 连通性·三(Tarjan缩点+暴力DFS)

    #1185 : 连通性·三 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 暑假到了!!小Hi和小Ho为了体验生活,来到了住在大草原的约翰家.今天一大早,约翰因为有事要出 ...

  2. Strange Country II 暴力dfs

    这题点的个数(<=50)有限, 所以可以纯暴力DFS去搜索 //#pragma comment(linker, "/STACK:16777216") //for c++ Co ...

  3. UVA129 暴力dfs,有许多值得学习的代码

    紫书195 题目大意:给一个困难的串,困难的串的定义就是里面没有重复的串. 思路:不需要重新对之前的串进行判重,只需要对当前的加入的字符进行改变即可. 因为是判断字典序第k个的字符串,所以要多一个全局 ...

  4. 2018杭电多校第五场1002(暴力DFS【数位】,剪枝)

    //never use translation#include<bits/stdc++.h>using namespace std;int k;char a[20];//储存每个数的数值i ...

  5. A. The Fault in Our Cubes 暴力dfs

    http://codeforces.com/gym/101257/problem/A 把它固定在(0,0, 0)到(2, 2, 2)上,每次都暴力dfs检查,不会超时的,因为规定在这个空间上,一不行, ...

  6. 【BZOJ】1675: [Usaco2005 Feb]Rigging the Bovine Election 竞选划区(暴力dfs+set判重)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1675 一开始我写了个枚举7个点....... 但是貌似... 写挫了. 然后我就写dfs.. 判重好 ...

  7. bzoj 4765 普通计算姬 dfs序 + 分块

    题目链接 Description "奋战三星期,造台计算机".小G响应号召,花了三小时造了台普通计算姬.普通计算姬比普通计算机要厉害一些.普通计算机能计算数列区间和,而普通计算姬能 ...

  8. 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 ...

  9. UVA 185(暴力DFS)

      Roman Numerals  The original system of writing numbers used by the early Romans was simple but cum ...

随机推荐

  1. LCD驱动程序(二)

    上节我们主要是对fb_info结构体的配置,对fb_info结构体的配置主要分为一下步骤: static int lcd_init(void){ /* 1. 分配一个fb_info */ s3c_lc ...

  2. struts2一个实例中遇到的问题

    今天实现了一个登录功能的Struts2小程序. 期间遇到了许多问题,记忆犹新的是 (1)新版本的tomcat9和eclipse Neon Release (4.6.0) 发生了冲突,启动服务器的时候老 ...

  3. EasyNVR无插件直播服务器软件如何自己更改web界面(网页的自定修改)

    背景需求 很多用户都在使用了EasyNVR,看到EasyNVR自身带有的界面后有这样的需求,就是需要更改一下web前端的一些样式,当前EasyhNVR为3.0版本,web前端为了增加前端的运行效率和减 ...

  4. 第1条:确认自己所用的Python版本

    很多电脑都预装了多个版本的标准CPython运行时环境,然而,在命令行中输入默认的python命令之后,究竟会执行哪一个版本无法肯定. python通常是python2.7的别名,但也有可能是pyth ...

  5. noVNC

    noNVC基础用法: 1.下载noVNC git clone https://github.com/novnc/noVNC.git 2.编辑qemu.conf配置文件 Vim /etc/libvirt ...

  6. PyVmomi Clone_VM with CustomizaitonSpec

    调用CustomizaitonSpec来Clone VM ''' Created on 2017-09-03 @author: Vincen ''' from pyVmomi import vim f ...

  7. poj 1028 Web Navigation 【模拟题】

    题目地址:http://poj.org/problem?id=1028 测试样例: Sample Input VISIT http://acm.ashland.edu/ VISIT http://ac ...

  8. 在webBrowser中取Cookie的方法

    在很多情况下我们会使用间进程的webBrowser去实现一些网页的请求和抓去,这个时候有部分网页是取不到Cookie的,那怎么办呢?下面我提供一个方法,应该99%的都能取到, //取当前webBrow ...

  9. hdu 3308 线段树 区间合并+单点更新+区间查询

    LCIS Time Limit: 6000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submis ...

  10. Storm- Storm作业提交运行流程

    用户编写Storm Topology 使用client提交Topology给Nimbus Nimbus指派Task给Supervisor Supervisor为Task启动Worker Worker执 ...