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. COGS 5. P服务点设置

    5. P服务点设置 http://www.cogs.pro/cogs/problem/problem.php?pid=5 ★★   输入文件:djsc.in   输出文件:djsc.out   简单对 ...

  2. a标签伪元素选择器

    a{ color: black; } /*未访问的链接*/ a:link{ color: red; } /*访问过的链接*/ a:visited{ color: green; } /*鼠标经过时*/ ...

  3. Java并发编程原理与实战三十九:JDK8新增锁StampedLock详解

    1.StampedLock是做什么的? ----->它是ReentrantReadWriteLock 的增强版,是为了解决ReentrantReadWriteLock的一些不足.   2.Ree ...

  4. Maven项目中通过profile定义使不同环境使用不同配置信息

    profile可以让我们定义一系列的配置信息,然后指定其激活条件.这样我们就可以定义多个profile,然后每个profile对应不同的激活条件和配置信息,从而达到不同环境使用不同配置信息的效果.比如 ...

  5. pywinauto: 导入时遇到 "TypeError: LoadLibrary() argument 1 must be string, not unicode"

    pywinauto: 导入时遇到 "TypeError: LoadLibrary() argument 1 must be string, not unicode" 经查询, 看到 ...

  6. 浅谈ASP.net中的DataSet对象

    在我们对数据库进行操作的时候,总是先把数据从数据库取出来,然后放到一个"容器"中,再通过这个"容器"取出数据显示在前台,而充当这种容器的角色中当属DataSet ...

  7. 通过Class类获取对象实例

    通过Class对象获取对象的方式是通过class.newInstance()方式获取,通过调用默认构造参数实例化一个对象. /** * Created by hunt on 2017/6/27. * ...

  8. python——脚本和print

    脚本和print 1.脚本文件 <Python 基础教程>(第二版)中 P118页,原操作为下: 1 _metaclass_ = type 2 3 class Person: 4 def ...

  9. Servlet笔记1--概述

    JavaEE概述及系统架构分析: (1) JavaEE概述: (2) 系统架构分析:

  10. linux用户权限 -> 系统用户管理

    用户基本概述: Linux用户属于多用户操作系统,在windows中,可以创建多个用户,但不允许同一时间多个用户进行系统登陆,但是Linux可以同时支持多个用户同时登陆操作系统,登陆后互相之间并不影响 ...