Cryptcowgraphy
Brian Dean

The cows of Farmer Brown and Farmer John are planning a coordinated escape from their respective farms and have devised a method of encryption to protect their written communications.

Specifically, if one cow has a message, say, "International Olympiad in Informatics", it is altered by inserting the letters C, O, and W, in random location in the message, such that C appears before O, which appears before W. Then the cows take the part of the message between C and O, and the part between O and W, and swap them. Here are two examples:

            International Olympiad in Informatics
->
CnOIWternational Olympiad in Informatics International Olympiad in Informatics
->
International Cin InformaticsOOlympiad W

To make matters more difficult, the cows can apply their encryption scheme several times, by again encrypting the string that results from the previous encryption. One night, Farmer John's cows receive such a multiply-encrypted message. Write a program to compute whether or not the non-encrypted original message could have been the string:

            Begin the Escape execution at the Break of Dawn

PROGRAM NAME: cryptcow

INPUT FORMAT

A single line (with both upper and lower case) with no more than 75 characters that represents the encrypted message.

SAMPLE INPUT (file cryptcow.in)

Begin the EscCution at the BreOape execWak of Dawn

OUTPUT FORMAT

Two integers on a single line. The first integer is 1 if the message decodes as an escape message; 0 otherwise. The second integer specifies the number of encryptions that were applied (or 0 if the first integer was 0).

SAMPLE OUTPUT (file cryptcow.out)

1 1

——————————————————————————————————————————题解
本来水了过去想看看题解
嗯,第一个题解464行,第二个题解465行
除掉一堆注释回车的话至少也200多
然后有点害怕,照着nocow的一堆剪枝写了写……
本来有个错误的想法是最后一次更新的C应该在最前面,也不知道我是怎么想的……水过4个点 这道题HASH会有冲突,可能舍掉一些不知道是不是正解的可能性解,但是这棵树太大了,胡乱砍掉的一些东西结果没有什么影响……题解里用的是hash挂链表
用ELFhash函数……一个看起来很玄妙的hash函数 然后再剪枝就是针对一个串C肯定是第一个出现,W肯定是最后一个出现,这些C,O,W出现的子串一定在目标串里出现过,暴力匹配 顺序也很重要,我们先中间后两边,O->C->W,其中W倒序搜索
 /*
ID: ivorysi
LANG: C++
PROG: cryptcow
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <set>
#include <vector>
#include <algorithm>
#define siji(i,x,y) for(int i=(x);i<=(y);++i)
#define gongzi(j,x,y) for(int j=(x);j>=(y);--j)
#define xiaosiji(i,x,y) for(int i=(x);i<(y);++i)
#define sigongzi(j,x,y) for(int j=(x);j>(y);--j)
#define inf 0x5f5f5f5f
#define ivorysi
#define mo 97797977
#define hash 974711
#define base 47
#define fi first
#define se second
#define pii pair<int,int>
#define esp 1e-8
typedef long long ll;
using namespace std;
string os="Begin the Escape execution at the Break of Dawn";
string a;
int st[][],pt[];
bool flag;
bool ha[hash+];
int id(char c) {
if('a'<=c && c<='z') return c-'a'+;
if('A'<=c && c<='Z') return c-'A'++;
else return ;
}
int ELFhash() {
int l=,len=a.length();
unsigned int h=,g;
while(l!=len) {
h=(h<<)+a[l];//右移四位加上一个字符
if(g=h&0xF0000000L) {//最高四位不为0
h^=(g>>);//最高四位和5~8位异或
h &= ~g;//删除最高四位
}
++l;
}
return h&0x7FFFFFFF;//处理成非负数
}
void get_st() {
int len=os.length();
xiaosiji(i,,len) {
st[id(os[i])][++pt[id(os[i])]]=i;
}
}
bool check_substr() {
int len=a.length();
int s=,t=;
for(int i=;i<len;++i) {
if(a[i]!='C' && a[i]!='O' && a[i]!='W') {
if(a[i]!=os[i]) return ;
}
else {
if(a[i]!='C') return ;
else {s=i;break;}
}
}
for(int i=len-,los=os.length()-;i>=;--i,--los) {
if(a[i]!='C' && a[i]!='O' && a[i]!='W') {
if(a[i]!=os[los]) return ;
}
else {
if(a[i]!='W') return ;
else {t=i;break;}
}
}
for(int i=s+;i<t;++i) { int rt=i,to;
while(rt!=len &&(a[rt]=='C' || a[rt]=='O' || a[rt]=='W') )++rt;
if(rt==len) break;
to=rt;
while(to!=len && a[to]!='C' && a[to]!='O' && a[to]!='W') ++to;
--to;
bool f=;
int pos=id(a[rt]);
siji(k,,pt[pos]) {
siji(j,rt,to) {
if(st[pos][k]+j-rt>=os.length() ||
a[j]!=os[st[pos][k]+j-rt]) goto fail;
}
f=;
break;
fail:;
}
if(!f) return ;
i=to;
}
return ;
}
bool dfs() {
if(a==os) return true;
int h=ELFhash()%hash;
if(ha[h]!=) return false;
ha[h]=;
int len=a.length();
siji(i,,len-) {
if(a[i]=='O') {
siji(j,,i) {
if(a[j]=='C') {
gongzi(k,len-,i+) {
if(a[k]=='W') {
string temp=a;
a.replace(j,k-j+,temp.substr(i+,k-i-)+temp.substr(j+,i-j-));
if(check_substr() && dfs()) return true;
a=temp;
}
}
}
} }
}
return false;
}
void solve() {
getline(cin,a);
get_st();
int len=a.length();
flag=;
int c=,o=,w=;
xiaosiji(i,,len) {
if(a[i]=='W') ++w;
if(a[i]=='C') ++c;
if(a[i]=='O') ++o;
}
if(c!=o || c!=w || w!=o) flag=;
if(len-c*!=os.length()) flag=;
if(flag) flag=check_substr(); if(!flag) {printf("0 0\n");return;}
flag=dfs();
if(flag) printf("1 %d\n",c);
else printf("0 0\n");
}
int main(int argc, char const *argv[])
{
#ifdef ivorysi
freopen("cryptcow.in","r",stdin);
freopen("cryptcow.out","w",stdout);
#else
freopen("f1.in","r",stdin);
#endif
solve();
return ;
}
 

USACO 6.3 Cryptcowgraphy的更多相关文章

  1. USACO 6.3 章节 你对搜索和剪枝一无所知QAQ

    emmm........很久很久以前 把6.2过了 所以emmmmmm 直接跳过 ,从6.1到6.3吧 Fence Rails 题目大意 N<=50个数A1,A2... 1023个数,每个数数值 ...

  2. USACO . Your Ride Is Here

    Your Ride Is Here It is a well-known fact that behind every good comet is a UFO. These UFOs often co ...

  3. 【USACO 3.1】Stamps (完全背包)

    题意:给你n种价值不同的邮票,最大的不超过10000元,一次最多贴k张,求1到多少都能被表示出来?n≤50,k≤200. 题解:dp[i]表示i元最少可以用几张邮票表示,那么对于价值a的邮票,可以推出 ...

  4. USACO翻译:USACO 2013 NOV Silver三题

    USACO 2013 NOV SILVER 一.题目概览 中文题目名称 未有的奶牛 拥挤的奶牛 弹簧牛 英文题目名称 nocow crowded pogocow 可执行文件名 nocow crowde ...

  5. USACO翻译:USACO 2013 DEC Silver三题

    USACO 2013 DEC SILVER 一.题目概览 中文题目名称 挤奶调度 农场航线 贝西洗牌 英文题目名称 msched vacation shuffle 可执行文件名 msched vaca ...

  6. USACO翻译:USACO 2014 DEC Silver三题

    USACO 2014 DEC SILVER 一.题目概览 中文题目名称 回程 马拉松 奶牛慢跑 英文题目名称 piggyback marathon cowjog 可执行文件名 piggyback ma ...

  7. USACO翻译:USACO 2012 FEB Silver三题

    USACO 2012 FEB SILVER 一.题目概览 中文题目名称 矩形草地 奶牛IDs 搬家 英文题目名称 planting cowids relocate 可执行文件名 planting co ...

  8. USACO翻译:USACO 2012 JAN三题(3)

    USACO 2012JAN(题目三) 一.题目概览 中文题目名称 放牧 登山 奶牛排队 英文题目名称 grazing climb lineup 可执行文件名 grazing climb lineup ...

  9. USACO翻译:USACO 2012 JAN三题(2)

    USACO 2012 JAN(题目二) 一.题目概览 中文题目名称 叠干草 分干草 奶牛联盟 英文题目名称 stacking baleshare cowrun 可执行文件名 stacking bale ...

随机推荐

  1. 11 Facts about Data Science that you must know

    11 Facts about Data Science that you must know Statistics, Machine Learning, Data Science, or Analyt ...

  2. 张鑫旭:Promise异步编程模式

    参考文章: http://www.zhangxinxu.com/wordpress/2014/02/es6-javascript-promise-%E6%84%9F%E6%80%A7%E8%AE%A4 ...

  3. Composer 自动加载(autoload)机制

    自动加载的类型 总体来说 composer 提供了几种自动加载类型 classmap psr-0 psr-4 files 这几种自动加载都会用到,理论上来说,项目代码用 psr-4 自动加载, hel ...

  4. Spring MVC表单防重复提交

    利用Spring MVC的过滤器及token传递验证来实现表单防重复提交. 创建注解 @Target(ElementType.METHOD) @Retention(RetentionPolicy.RU ...

  5. JS中的作用域和闭包

    作用域:在编程语言中,作用域控制着变量与参数的可见性及生命周期.JS确实有函数作用域,那意味着定义在函数中的参数和变量在函数外部是不可见的,而且在一个函数中的任何位置定义的变量在该函数中的任何地方都是 ...

  6. Is It A Tree? 挂着并查集的帽子招摇撞骗

    Description A tree is a well-known data structure that is either empty (null, void, nothing) or is a ...

  7. 【leetcode 简单】 第七十题 有效的字母异位词

    给定两个字符串 s 和 t ,编写一个函数来判断 t 是否是 s 的一个字母异位词. 示例 1: 输入: s = "anagram", t = "nagaram" ...

  8. vue_列表渲染

    vue列表渲染 <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <t ...

  9. ORB_SLAM2 源码阅读 ORB_SLAM2::Initializer::ComputeF21 (OpenCV 细节)

    ORB_SLAM2 计算 F21 的代码是这样的. cv::Mat Initializer::ComputeF21(const vector<cv::Point2f> &vP1,c ...

  10. 读后感+资源-----java8函数式编程pdf

    花了两周时间工作之余抽空读完了这本书,对lamdba以及java的理解又有了一个新的认识(装个逼,哈哈) 以前看视频学习的还是太基本了,感觉读书更容易理解背后的设计思想和编程思路 这本书还是挺不错,就 ...