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 coursera 机器学习编程作业 exercise 3(使用神经网络 识别手写的阿拉伯数字(0-9))

    本作业使用神经网络(neural networks)识别手写的阿拉伯数字(0-9) 关于使用逻辑回归实现多分类问题:识别手写的阿拉伯数字(0-9),请参考:http://www.cnblogs.com ...

  2. Oracle过程及函数的参数模式,In、out、in out模式

    Oracle过程及函数的参数模式 In.out.in out模式 在Oracle中过程与函数都可以有参数,参数的类型可以指定为in.out.in out三种模式. 三种参数的具体说明,如下图所示: ( ...

  3. iOS ARC内存管理

    iOS的内存管理机制,只要是iOS开发者,不管多长的时间经验,都能说出来一点,但是要深入的理解.还是不简单的.随着ARC(自动管理内存)的流行.iOS开发者告别了手动管理内存的复杂工作.但是自动管理内 ...

  4. C语言:void指针

    使用前必须进行强制类型转换 #include <stdio.h> void test(const void *p); int main(){ ; int *p = &i; puts ...

  5. Maven 常用命令, 备忘

    Maven在现在的Java项目中有非常重要的地位, Maven已经不是Ant这样仅仅用于构建, 首先, 它是一个构建工具, 把源代码编译并打包成可发布应用的构件工具其次, 它是一个依赖管理工具, 集中 ...

  6. PHP & Delphi 語法

    明 C(区分大小写) Delphi(不区分大小写) PHP(区分大小写) 整型变量的定义 1 2 3 4 5 6 7 char a = 'a';         /* 8位有符号*/ int a=10 ...

  7. Android手机浏览器访问本地网络相关问题

    为了测试开发的手机网站,常常需要使手机直接访问本地网络. 在这个过程中碰到几个问题,记下来供以后参考 1. 在本地主机运行apache后,使用localhost和127.0.0.1可以访问页面,但使用 ...

  8. 【HTML5+MVC4】xhEditor网页编辑器图片上传

    准备工作: 创建一个MVC项目中,添加好xhEditor插件 相关用法:http://www.cnblogs.com/xcsn/p/4701497.html 注意事项:xhEditor分为v1.1.1 ...

  9. Linux shell基础

    shell是核心程序kernel之外的指令解析器,是一个程序,同事是一种命令语言和程序设计语言 --shell是命令解析器,用户输入命令,它去解析. shell类型 ash,bash,ksh,csh, ...

  10. UML:类图复习-鸡生蛋,蛋生鸡

    这是前一阵<高级软件工程>课堂上,老师随堂出的一道讨论题,随手贴在这里: ps: 今天是520,正好聊一些OoXx,关于爱的扯淡话题:) 题目:“鸡生蛋,蛋孵鸡”,世间万物生生不息,如何用 ...