USACO 6.3 Cryptcowgraphy
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的更多相关文章
- USACO 6.3 章节 你对搜索和剪枝一无所知QAQ
emmm........很久很久以前 把6.2过了 所以emmmmmm 直接跳过 ,从6.1到6.3吧 Fence Rails 题目大意 N<=50个数A1,A2... 1023个数,每个数数值 ...
- 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 ...
- 【USACO 3.1】Stamps (完全背包)
题意:给你n种价值不同的邮票,最大的不超过10000元,一次最多贴k张,求1到多少都能被表示出来?n≤50,k≤200. 题解:dp[i]表示i元最少可以用几张邮票表示,那么对于价值a的邮票,可以推出 ...
- USACO翻译:USACO 2013 NOV Silver三题
USACO 2013 NOV SILVER 一.题目概览 中文题目名称 未有的奶牛 拥挤的奶牛 弹簧牛 英文题目名称 nocow crowded pogocow 可执行文件名 nocow crowde ...
- USACO翻译:USACO 2013 DEC Silver三题
USACO 2013 DEC SILVER 一.题目概览 中文题目名称 挤奶调度 农场航线 贝西洗牌 英文题目名称 msched vacation shuffle 可执行文件名 msched vaca ...
- USACO翻译:USACO 2014 DEC Silver三题
USACO 2014 DEC SILVER 一.题目概览 中文题目名称 回程 马拉松 奶牛慢跑 英文题目名称 piggyback marathon cowjog 可执行文件名 piggyback ma ...
- USACO翻译:USACO 2012 FEB Silver三题
USACO 2012 FEB SILVER 一.题目概览 中文题目名称 矩形草地 奶牛IDs 搬家 英文题目名称 planting cowids relocate 可执行文件名 planting co ...
- USACO翻译:USACO 2012 JAN三题(3)
USACO 2012JAN(题目三) 一.题目概览 中文题目名称 放牧 登山 奶牛排队 英文题目名称 grazing climb lineup 可执行文件名 grazing climb lineup ...
- USACO翻译:USACO 2012 JAN三题(2)
USACO 2012 JAN(题目二) 一.题目概览 中文题目名称 叠干草 分干草 奶牛联盟 英文题目名称 stacking baleshare cowrun 可执行文件名 stacking bale ...
随机推荐
- Hadoop生态圈-hive五种数据格式比较
Hadoop生态圈-hive五种数据格式比较 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.
- java 编码问题
Java默认使用Unioncode编码,即不论什么语言都是一个字符占两个字节 Java的class文件编码为UTF-8,而虚拟机JVM编码为UTF-16 UTF-8编码下,一个中文占3个字节,一个英文 ...
- H5页面中唤起native app
现在各类app,分享出去的H5页面中,一般都会带着一个立即打开的按钮,如果本地安装了app,那么就直接唤起本地的app,如果没有安装,则跳转到下载.这是一个很正常的推广和导流量的策略,最近产品经理就提 ...
- Jupyter Notebook Tutorial: Introduction, Setup, and Walkthrough
Jupyter Notebook Tutorial: Introduction, Setup, and Walkthrough YouTube https://www.youtube.com/watc ...
- sublime text3 最常用的快捷键及插件
A:最常用的快捷键 Tab:自动补齐代码 <!--div+Tab 其它标签一样--><div></div> emmet常用的使用方法 <!--ul>li ...
- protobuffer
[protobuffer] 1.扩展名为.proto. 2.定义一个协议: 3.定义一个Service: 4.编译器为protoc,使用protoc: 5.style:所有的类型名均CamelCase ...
- AngularJS - 下一个大框架
AngularJS AngularJS是web应用的下一个巨头. AngularJS如果为创建web应用而设计,那它就是HTML的套路了.具有数据绑定, MVW, MVVM, MVC, 依赖注入的声明 ...
- 天梯赛 L2-024. (并查集) 部落
题目链接 题目描述 在一个社区里,每个人都有自己的小圈子,还可能同时属于很多不同的朋友圈.我们认为朋友的朋友都算在一个部落里,于是要请你统计一下,在一个给定社区中,到底有多少个互不相交的部落?并且检查 ...
- 树形dp(A - Anniversary party HDU - 1520 )
题目链接:https://cn.vjudge.net/contest/277955#problem/A 题目大意:略 具体思路:刚开始接触树形dp,说一下我对这个题的初步理解吧,首先,我们从根节点开始 ...
- 编写pl/sql时,报错
/* 写一个简单的PL/SQL */ declare a ; b ; c number; begin c:=(a+b)/(a-b); dbms_output.put_line(c); exceptio ...