http://codevs.cn/problem/2083/

奶牛搜索题。我加了如下剪枝:

1.用字符串hash判重。注意判重时也要对字符串长度判重,否则会出现两个字符串长度不同但hash值相同的情况,非常容易被卡hash。

2.先枚举O,再枚举C,W。枚举W时从后往前枚举。从网上的题解里看到的,具体为什么我也不知道(捂脸)。

3.对于所有的C,O,W字母可以将字符串切成好几段,但是无论我们怎么交换C-O之间和O-W之间的字符串,这些小段字符串是不会被分开的。所以对于每一个状态我们判断所有C,O,W切成的小段是否在目标串中出现过。这是非常厉害的剪枝。

4.剪枝3只是判断某些字符串小段是否在目标串中出现过,还可以对这些小段的第一段和最后一段加强限制。即当前串的最小的结尾字符的后一个字符是C的前缀必须是目标串的前缀,当前串最小的开头字符的前一个字符是W的后缀必须是目标串的后缀。这也是非常重要的剪枝。

有了上面四个剪枝就可以通过此题了。貌似少一个剪枝就会TLE。

搜索难在分析其玄学的复杂度QAQ

#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
typedef long long ll;
const int w = 140;
const int p = 1000007;
const char s[48] = {' ','B','e','g','i','n',' ','t','h','e',' ','E','s','c','a','p','e',' ','e','x','e','c','u','t','i','o','n',' ','a','t',' ','t','h','e',' ','B','r','e','a','k',' ','o','f',' ','D','a','w','n'}; bool showed[9][p + 3];
int n, Pw[80], hash[50], step; int gethash(int l, int r) {
return (int) ((1ll * hash[r] - 1ll * hash[l - 1] * Pw[r - l + 1] % p + p) % p);
} bool flag = false; bool shown(int ha, int len) {
for(int i = len; i <= 47; ++i)
if (gethash(i - len + 1, i) == ha) return true;
return false;
} void dfs(int tmp, char *c, int tot) {
if (tmp > step) {
for(int i = 1; i <= 47; ++i)
if (s[i] != c[i]) return;
flag = true; return;
} int ret = 0;
for(int i = 1; i <= tot; ++i) ret = (int) ((1ll * ret * w + (int) c[i]) % p);
if (showed[tmp - 1][ret]) return;
showed[tmp - 1][ret] = true; ret = 1;
while (c[ret] != 'C' && c[ret] != 'O' && c[ret] != 'W' && ret <= tot) {
if (c[ret] != s[ret]) ret = tot + 2;
++ret;
}
if (ret == tot + 2) return;
if (ret != tot + 1 && c[ret] != 'C') return;
ret = 0;
while (c[tot - ret] != 'C' && c[tot - ret] != 'O' && c[tot - ret] != 'W' && ret < tot) {
if (c[tot - ret] != s[47 - ret]) ret = tot + 2;
++ret;
}
if (ret == tot + 2) return;
if (ret != tot && c[tot - ret] != 'W') return; int head = 1;
for(int i = 1; i <= tot; ++i)
if (c[i] == 'C' || c[i] == 'O' || c[i] == 'W') {
if (head < i) {
ret = 0;
for(int j = head; j < i; ++j) ret = (int) ((1ll * ret * w + (int) c[j]) % p);
if (!shown(ret, i - head)) return;
}
head = i + 1;
} char ch[76];
for(int i = 1; i <= tot; ++i)
if (c[i] == 'O')
for(int j = 1; j < i; ++j)
if (c[j] == 'C')
for(int k = tot; k > i; --k)
if (c[k] == 'W') {
ret = 0;
for(int l = 1; l < j; ++l) ch[++ret] = c[l];
for(int l = i + 1; l < k; ++l) ch[++ret] = c[l];
for(int l = j + 1; l < i; ++l) ch[++ret] = c[l];
for(int l = k + 1; l <= tot; ++l) ch[++ret] = c[l];
dfs(tmp + 1, ch, ret);
if (flag) return;
}
} char c[80]; int main() {
gets(c + 1);
n = strlen(c + 1);
Pw[0] = 1;
for(int i = 1; i <= n; ++i)
Pw[i] = (int) (1ll * Pw[i - 1] * w % p);
hash[0] = 0;
for(int i = 1; i <= 47; ++i)
hash[i] = (int) ((1ll * hash[i - 1] * w + (int) s[i]) % p); step = (n - 47) / 3;
dfs(1, c, n); if (flag) printf("%d %d\n", 1, step);
else puts("0 0"); return 0;
}

调了一下午+一晚上

【CodeVS 2083】Cryptcowgraphy 解密牛语的更多相关文章

  1. 163. [USACO Mat07] 牛语

    ☆   输入文件:latin.in   输出文件:latin.out   简单对比 时间限制:1 s   内存限制:128 MB 译: zqzas 奶牛们听说猪发明了一种秘密语言,叫做"猪语 ...

  2. COGS 163 [USACO Mat07] 牛语

    COGS 163 [USACO Mat07] 牛语 输入文件:latin.in   输出文件:latin.out   简单对比 时间限制:1 s   内存限制:128 MB 奶牛们听说猪发明了一种秘密 ...

  3. bzoj1633 [Usaco2007 Feb]The Cow Lexicon 牛的词典

    Description 没有几个人知道,奶牛有她们自己的字典,里面的有W (1 ≤ W ≤ 600)个词,每个词的长度不超过25,且由小写字母组成.她们在交流时,由于各种原因,用词总是不那么准确.比如 ...

  4. BZOJ 1633: [Usaco2007 Feb]The Cow Lexicon 牛的词典

    题目 1633: [Usaco2007 Feb]The Cow Lexicon 牛的词典 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 401  Solv ...

  5. [USACO07FEB]牛的词汇The Cow Lexicon

    https://daniu.luogu.org/problemnew/show/P2875 dp[i]表示前i-1个字符,最少删除多少个 枚举位置i, 如果打算从i开始匹配, 枚举单词j,计算从i开始 ...

  6. 【BZOJ】1633: [Usaco2007 Feb]The Cow Lexicon 牛的词典(dp)

    http://www.lydsy.com/JudgeOnline/problem.php?id=1633 一开始也想到了状态f[i]表示i以后的字符串最少删的数 然后想到的转移是 f[i]=min{f ...

  7. 洛谷P2875 [USACO07FEB]牛的词汇The Cow Lexicon

    P2875 [USACO07FEB]牛的词汇The Cow Lexicon 题目描述 Few know that the cows have their own dictionary with W ( ...

  8. 【题解】Luogu P2875 [USACO07FEB]牛的词汇The Cow Lexicon

    题目描述 Few know that the cows have their own dictionary with W (1 ≤ W ≤ 600) words, each containing no ...

  9. 2272: [Usaco2011 Feb]Cowlphabet 奶牛文字

    2272: [Usaco2011 Feb]Cowlphabet 奶牛文字 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 138  Solved: 97 ...

随机推荐

  1. Stanford机器学习笔记-4. 神经网络Neural Networks (part one)

    4. Neural Networks (part one) Content: 4. Neural Networks (part one) 4.1 Non-linear Classification. ...

  2. SSM三大框架整合详细教程(Spring+SpringMVC+MyBatis)(转)

    使用 SSM ( Spring . SpringMVC 和 Mybatis )已经有三个多月了,项目在技术上已经没有什么难点了,基于现有的技术就可以实现想要的功能,当然肯定有很多可以改进的地方.之前没 ...

  3. [转]基于display:table的CSS布局

    当IE8发布时,它将支持很多新的CSS display属性值,包括与表格相关的属性值:table.table-row和table-cell,它也是最后一款支持这些属性值的主流浏览器.它标志着复杂CSS ...

  4. POJ1160 Post Office[序列DP]

    Post Office Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 18680   Accepted: 10075 Des ...

  5. Thread对象的yield(),wait(),notify(),notifyall()

    Thread类中的主要方法: join()方法:让一个线程强制运行,线程强制运行期间,其他线程无法运行,必须等到此线程完成之后才可以继续执行. setDaemon():设置线程为后台线程,这样即使Ja ...

  6. sqlmap小白操作

    转载地儿:http://blog.csdn.net/zgyulongfei/article/details/41017493 对于网络安全人员来说,掌握渗透工具的使用方法是一项必备的技能.然而,一个没 ...

  7. android 调用系统的音乐和视频播放器

    package com.eboy.testsystemaudiovideo; import android.app.Activity;import android.content.Intent;imp ...

  8. pullRefresh组件配置

    mui.init({ pullRefresh:{ container: '#contanier', indicators:false, up:{ height:200, contentinit: '' ...

  9. mui禁止滚动条和禁止滚动

    mui.plusReady(function () { plus.webview.currentWebview().setStyle({ scrollIndicator: 'none' }); }); ...

  10. linux运维中的命令梳理(三)

    ----------文本操作命令---------- sed命令:文本编辑工具 sed是一个很好的文件处理工具,本身是一个管道命令,主要是以行为单位进行处理,可以将数据行进行替换.删除.新增.选取等特 ...