您的位置:首页 > 其它

USACO6.3.2 Cryptcowgraph(cryptcow)

2015-02-05 21:00 253 查看
模拟

枚举,先找O再找C最后找W会快很多,转换,判断

这种题需要注意细节上的问题

/*
ID:10239512
PROG:cryptcow
LANG:C++
*/
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
#define HashSize 131071
const string dest="Begin the Escape execution at the Break of Dawn";

bool searched[HashSize];

int Hash(const string &str)
{
long long h=0,g;
for(int i=0;i<str.size();i++)
{
h=(h<<4)+str[i];
g=h & 0xf0000000l;
if(g)    h^=g>>24;

h&=~g;
}
return h;
}

bool Impossible(const string &text)
{
if((text.size()-dest.size())%3)    return 1;

int i=0,j;
while(i<text.size())
{
j=i+1;
if(text[i]!='C' && text[i]!='O' && text[i]!='W')
{
while(j<text.size())
{
if (text[j]=='C' || text[j]=='O' || text[j]=='W')
break;
j++;
}

if (dest.find(text.substr(i,j-i))==string::npos)
return 1;
}
i=j;
}

return 0;
}

string Transform(const string &str,int c,int o,int w)
{
static char temp[100];
int ich=0;
for(int i=0;i<c;i++)
temp[ich++]=str[i];
for(int i=o+1;i<w;i++)
temp[ich++]=str[i];
for(int i=c+1;i<o;i++)
temp[ich++]=str[i];
for (int i=w+1;i<str.size();i++)
temp[ich++]=str[i];

temp[ich++]=0;

return string(temp);
}

bool IsEncrypted(string text)
{
int hash=Hash(text)%HashSize;
if (searched[hash]) return 0;
searched[hash]=1;

if(text==dest)  return 1;
if (Impossible(text))   return 0;

for(int o=1; o<text.size()-1; o++)
if(text[o]=='O')
for(int c=0; c<o; c++)
if(text[c]=='C')
for(int w=text.size()-1; w>o; w--)
if(text[w]=='W')
if (IsEncrypted(Transform(text,c,o,w)))
return 1;
return 0;
}

int main()
{
freopen("cryptcow.in","r",stdin);
freopen("cryptcow.out","w",stdout);

string text;
getline(cin,text);

if(IsEncrypted(text))
cout<<"1 "<<count(text.begin(),text.end(),'C')<<endl;
else
cout<<"0 0"<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: