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 ...
随机推荐
- 获取异常信息e.printStackTrace()的内容
获取异常信息e.printStackTrace()的内容 最近做项目的时候需要记录操作的日志,但是记录异常信息的是发现使用e.getMessage()根本无法满足需要,并且e.getMessage() ...
- 遗传算法入门C1
遗传算法入门C1 觉得有用的话,欢迎一起讨论相互学习~Follow Me 参考文献 遗传算法历史 遗传算法(GA)是从生物进化的角度考虑提出来的方法,19世纪达尔文在大量观察基础上总结了大自然进化规律 ...
- Machine Learning Trick of the Day (2): Gaussian Integral Trick
Machine Learning Trick of the Day (2): Gaussian Integral Trick Today's trick, the Gaussian integral ...
- MongoDB - Introduction to MongoDB, Databases and Collections
MongoDB stores BSON documents, i.e. data records, in collections; the collections in databases. Data ...
- HDU 1176 排列2 全排列
解题报告:给出四个数,然后要你把这四个数组合成的各不相同的四位数按照从小到大的顺序输出来,然后如果最高位是0的话不能输出来,还有最高位是数字如果一样的话,则放在同一行输出. 本来是个比较简单的生成全排 ...
- 倍增 Tarjan 求LCA
...
- Hive笔记之导出查询结果
一.导出到本地 导出查询结果到本地: INSERT OVERWRITE LOCAL DIRECTORY "/tmp/hive-result/t_visit_video" SELEC ...
- package.json安装依赖的箭头?
- c# 生成随机N位数字串(每位可以重复)
/// <summary> /// 生成随机数字窜 /// </summary> /// <param name="Digit">位数</ ...
- 安裝HA服務
**************************************************************************************************** ...