USACO6.3.2 Cryptcowgraph(cryptcow)

模拟

枚举,先找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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值