【CodeVS 2083】Cryptcowgraphy 解密牛语
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 解密牛语的更多相关文章
- 163. [USACO Mat07] 牛语
☆ 输入文件:latin.in 输出文件:latin.out 简单对比 时间限制:1 s 内存限制:128 MB 译: zqzas 奶牛们听说猪发明了一种秘密语言,叫做"猪语 ...
- COGS 163 [USACO Mat07] 牛语
COGS 163 [USACO Mat07] 牛语 输入文件:latin.in 输出文件:latin.out 简单对比 时间限制:1 s 内存限制:128 MB 奶牛们听说猪发明了一种秘密 ...
- bzoj1633 [Usaco2007 Feb]The Cow Lexicon 牛的词典
Description 没有几个人知道,奶牛有她们自己的字典,里面的有W (1 ≤ W ≤ 600)个词,每个词的长度不超过25,且由小写字母组成.她们在交流时,由于各种原因,用词总是不那么准确.比如 ...
- BZOJ 1633: [Usaco2007 Feb]The Cow Lexicon 牛的词典
题目 1633: [Usaco2007 Feb]The Cow Lexicon 牛的词典 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 401 Solv ...
- [USACO07FEB]牛的词汇The Cow Lexicon
https://daniu.luogu.org/problemnew/show/P2875 dp[i]表示前i-1个字符,最少删除多少个 枚举位置i, 如果打算从i开始匹配, 枚举单词j,计算从i开始 ...
- 【BZOJ】1633: [Usaco2007 Feb]The Cow Lexicon 牛的词典(dp)
http://www.lydsy.com/JudgeOnline/problem.php?id=1633 一开始也想到了状态f[i]表示i以后的字符串最少删的数 然后想到的转移是 f[i]=min{f ...
- 洛谷P2875 [USACO07FEB]牛的词汇The Cow Lexicon
P2875 [USACO07FEB]牛的词汇The Cow Lexicon 题目描述 Few know that the cows have their own dictionary with W ( ...
- 【题解】Luogu P2875 [USACO07FEB]牛的词汇The Cow Lexicon
题目描述 Few know that the cows have their own dictionary with W (1 ≤ W ≤ 600) words, each containing no ...
- 2272: [Usaco2011 Feb]Cowlphabet 奶牛文字
2272: [Usaco2011 Feb]Cowlphabet 奶牛文字 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 138 Solved: 97 ...
随机推荐
- Sql动态添加字段的正确姿势
如何给指定表动态添加字段? 一.创建一张表[Tbl_AutoFileds] (tableName表名,fieldName字段名,dataType数据类型,length长度 isnull 是否允许为n ...
- Stanford coursera Andrew Ng 机器学习课程编程作业(Exercise 1)
Exercise 1:Linear Regression---实现一个线性回归 在本次练习中,需要实现一个单变量的线性回归.假设有一组历史数据<城市人口,开店利润>,现需要预测在哪个城市中 ...
- Unity 碰撞器
Unity引擎的碰撞器 常见的有Box Collider,Capsule Collider等等 IsTrigger(触发器) 在这些碰撞器中有一个属性 Is Trigger(是否使用触发器) 触发器事 ...
- java 22 - 18 多线程之 线程的状态转换、线程组
线程的状态转换图解:图片 线程的线程组: 线程组: 把多个线程组合到一起. 它可以对一批线程进行分类管理,Java允许程序直接对线程组进行控制. 首先创建一个Runnable的实现类 publi ...
- vijos1334 NASA的食物计划(二维费用的背包问题)
背景 NASA(美国航空航天局)因为航天飞机的隔热瓦等其他安 全技术问题一直大伤脑筋,因此在各方压力下终止了航天 飞机的历史,但是此类事情会不会在以后发生,谁也无法 保证,在遇到这类航天问题时,解决方 ...
- 用AutoCompleteTextView实现历史记录提示
自定义AutoCompleteTextView 博客分类: android进阶 android 网上找到的都是同ArrayAdapter一起使用的,有时候需要自定义风格,咋办?follow me! ...
- git中的版本回退
git版本回退有两种情况,一种是从本地版本库中(head区)回退到某个版本,可以用命令 git reset --hard head^ 或git reset --hard head~x ,head指的是 ...
- xpath 参考
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Te ...
- Codevs 1051 二叉树最大宽度和高度
1501 二叉树最大宽度和高度 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 白银 Silver 题目描述 Description 给出一个二叉树,输出它的最大宽 ...
- U3D屏幕空间到世界空间变换
using UnityEngine; using System.Collections; public class FPSCam : MonoBehaviour { Vector3 lastPos; ...