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 ...
随机推荐
- Broker流量均衡 prefer reassign
0.均衡流量的步骤 现在的kafka集群,只要遇到过weibo_common_act2 topic的节点在ZK中丢失,就要prefer一次流量,否则不均匀. 总结均衡流量的一般步骤: 通过hpm查询b ...
- 监控Elasticsearch的插件【check_es_system】
监控Elasticsearch的插件推荐 强大的shell脚本 #!/bin/bash ####################################################### ...
- linux查看及设置别名,权限,生成ssh秘钥
1.alias :查看系统中所有的命令别名 2.设定别名 alias 别名='原命令' 3.删除别名 unalias 别名 4.使别名永久生效 vi ~/.bashrc 写入这个文件中即可永 ...
- http Get和Post请求方式
string postURL ="http://xxxxx.ashx"; List<string> paramName = new List<string&g ...
- 《区块链100问》第73集:达世币Dash是什么?
达世币诞生于2014年1月18日,匿名程度较比特币更高. 达世币有三种转账方式,一是像比特币一样的普通转账:二是即时交易.不需要矿工打包确认,就可以确认交易,几乎可以实现秒到:三是匿名交易.从区块链上 ...
- 使用vue的一些经验
虽然说VUE是数据驱动视图的框架,但有时候不得不获取DOM来获得一些样式属性,做一些操作,这时候就需要VUE获取DOM对象的方法. vue获取DOM对象的方法: 如果是操作组件内部的DOM,可以通过给 ...
- 【工具记录】Linux口令破解
1.基础知识 /etc/passwd:记录着用户的基本属性,所有用户可读 字段含义如下: 用户名:口令:用户标识号:组标识号:注释性描述:主目录:登录Shell eg: root:x:0:0:root ...
- Linux下如何在进程中获取虚拟地址对应的物理地址【转】
转自:http://blog.csdn.net/kongkongkkk/article/details/74366200 如果让你编写一个程序,来获取虚拟地址对应的物理地址..你会试着操作MMU吗.. ...
- nginx_upstream_check_module-master对nginx的后端机器进行健康状态检查报403错误【转】
在nginx.conf配置文件中 在server添加 location /nstatus { check_status; access_log off; #allow 192.168.2.11; #d ...
- oracle11g 创建id自增长监听器的步骤与问题
首先,我们通过sql/plus先建个TEST表 sql语句: CTEATE TABLE TEST( ID NUMBER, NAME VARCHAR2(20), PRIMARY KEY(ID) ); 通 ...