题目大意:

定义字符串的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. Java语言实现简单FTP软件------>本地文件管理模块的实现(九)

    首先看一下界面: 1.本地文件列表的显示功能 将本地的当前目录下所有文件显示出来,并显示文件的属性包括文件名.大小.日期.通过javax.swing.JTable()来显示具体的数据.更改当前文件目录 ...

  2. linux c编程:线程互斥二 线程死锁

    死锁就是不同的程序在运行时因为某种原因发生了阻塞,进而导致程序不能正常运行.阻塞程序的原因通常都是由于程序没有正确使用临界资源. 我们举个日常生活中的例子来比喻死锁.我们把马路上行驶的汽车比作运行着的 ...

  3. web audio living

    总结网页音频直播的方案和遇到的问题. 代码:(github,待整理) 结果: 使用opus音频编码,web audio api 播放,可以达到100ms以内延时,高质量,低流量的音频直播. 背景: V ...

  4. sql获取数组长度

    需求:获取字符串数组1,2,3,4的长度,当然也可以是其他分隔符1|2|3等 方法:通过自定义函数来实现 /* 获取字符串数组长度 */ from sysobjects where id = obje ...

  5. 改善程序与设计的55个具体做法 day5

    条款12:复制对象时勿忘其每一个成分 这里的复制是拷贝构造和operator= 每一个成分有几个维度: 1.每个成员变量 这个很好理解,添加新的成员时也要记得为每个新添加的成员执行合适的复制操作 2. ...

  6. 图片加载ImageLoader

    https://github.com/nostra13/Android-Universal-Image-Loader public class AtguiguApplication extends A ...

  7. Linux Shell 下的输出重定向

    linux 环境中支持输入输出重定向,用符号<和>来表示. 0.1和2分别表示标准输入.标准输出和标准错误信息输出, 可以用来指定需要重定向的标准输入或输出,比如 2>a.txt 表 ...

  8. Swift URL encode

    前言 在WEB前端开发,服务器后台开发,或者是客户端开发中,对URL进行编码是一件很常见的事情,但是由于各个年代的RFC文档中的内容一直在变化,一些年代久远的代码就对URL编码和解码的规则和现在的有一 ...

  9. Video Brightness Enhancement

    Tone Mapping原是摄影学中的一个术语,因为打印相片所能表现的亮度范围不足以表现现实世界中的亮度域,而如果简单的将真实世界的整个亮度域线性压缩到照片所能表现的亮度域内,则会在明暗两端同时丢失很 ...

  10. 每天一个Linux命令(3)mkdir命令

        mkdir命令用来创建目录.    (1)用法:  用法: mkdir [选项]... 目录... (2)功能: 功能: 若指定目录不存在则创建目录 该命令创建由dirname命名的目录.如果 ...