记录编号 |
199268 |
评测结果 |
AAAAAAAAAA |
题目名称 |
解密牛语 |
最终得分 |
100 |
用户昵称 |
张灵犀不和我一般见识真可怕呢(笑 |
是否通过 |
通过 |
代码语言 |
C++ |
运行时间 |
1.661 s |
提交时间 |
2015-10-26 15:14:10 |
内存使用 |
1.27 MiB |
显示代码纯文本
- #include<cstdio>
- #include<string>
- #include<iostream>
- using namespace std;
- const int MOD=1000003;
- typedef long long LL;
- string E="\0";
- string txt;
- int C=0,O=0,W=0,S=0,T,End=0;
- bool h[MOD]={0};
- void read()
- {
- E="Begin the Escape ex";
- E+="ecution at the Break of Dawn";
- getline(cin,txt);
- }
- int BKDhash(string A)
- {
- LL seed=131313;
- LL ans=0;
- for(int i=0;i<A.size();i++)
- {
- ans+=ans*seed+(int)A[i];
- ans%=MOD;
- }
- return ans%MOD;
- }
- string change(string A,int c,int o,int w)
- {
- string ans;
- for(int i=0;i<c;i++) ans+=A[i];
- for(int i=o+1;i<w;i++) ans+=A[i];
- for(int i=c+1;i<o;i++) ans+=A[i];
- for(int i=w+1;i<A.size();i++) ans+=A[i];
- return ans;
- }
- bool legal(string A)
- {
- int lens=A.size(),len=0,i=0;
- while(len<lens&&A[len]!='C')
- {
- if(A[len]!=E[len]) return 0;
- len++;
- }
- int st=len,ed;
- i=E.size()-1;len=A.size()-1;
- while(i>=0&&len>=0&&A[len]!='W')
- {
- if(A[len]!=E[i]) return 0;
- len--,i--;
- }
- ed=len;
- while(st<ed)
- {
- string now;
- st++;
- bool flag=0;
- while(st<ed&&A[st]!='C'&&A[st]!='O'&&A[st]!='W')
- {
- now+=A[st];
- st++;
- }
- if(now.size()==0) flag=1;
- //cout<<A<<" "<<now<<endl;
- for(int j=0;j<E.size()-now.size();j++)
- {
- for(int k=j;k<j+now.size();k++)
- {
- if(E[k]!=now[k-j]) break;
- if(k==j+now.size()-1) flag=1;
- }
- if(flag==1) break;
- }
- if(flag==0) return 0;
- }
- return 1;
- }
- bool pan(string now)
- {
- bool flag=0;
- if(now.size()==0) flag=1;
- for(int j=0;j<=E.size()-now.size();j++)
- {
- for(int k=j;k<j+now.size();k++)
- {
- if(E[k]!=now[k-j]) break;
- if(k==j+now.size()-1) flag=1;
- }
- if(flag==1) break;
- }
- //cout<<now<<" "<<flag<<endl;
- if(flag==0) return 0;
- return 1;
- }
- string get(string A,int p,int q)
- {
- int i=p;
- int st=i-1;
- int len=A.size();
- while(st>=0&&A[st]!='C'&&A[st]!='O'&&A[st]!='W') st--;
- st++;
- string ans;
- for(int j=st;j<i;j++) ans+=A[j];
- st=q+1;
- while(st<len&&A[st]!='C'&&A[st]!='O'&&A[st]!='W')
- {
- ans+=A[st];
- st++;
- }
- return ans;
- }
- bool check(string A,int c,int o,int w)
- {
- int len=A.size();
- string ans;
- ans=get(A,c,o);if(!pan(ans)) return 0;
- ans=get(A,o,w);if(!pan(ans)) return 0;
- ans=get(A,w,c);if(!pan(ans)) return 0;
- //cout<<ans<<endl;
-
- return 1;
- }
- bool dfs(string A,int step)
- {
- string now;
- //cout<<A<<" "<<step<<endl;
- int Hash=BKDhash(A);
- if(h[Hash]) return 0;
- else if(A==E)
- {
- cout<<1<<" "<<step<<endl;
- return 1;
- }
- h[Hash]=1;
- if(!legal(A)) return 0;
- for(int o=0;o<A.size();o++)
- {
- if(A[o]!='O') continue;
- for(int c=0;c<o;c++)
- {
- if(A[c]!='C') continue;
- for(int w=A.size()-1;w>o;w--)
- {
- if(A[w]!='W') continue;
- string now;
- //cout<<A<<" "<<c<<" "<<o<<" "<<w<<endl;
- if(!check(A,c,o,w)) continue;
- now=change(A,c,o,w);
- //cout<<now<<endl;
- //cout<<endl;
- if(dfs(now,step+1)) return 1;
- }
- }
- }
- return 0;
- }
- void work()
- {
- End=BKDhash(E);
- //cout<<End<<endl;
- if((txt.size()-E.size())%3!=0||!dfs(txt,0)) printf("0 0\n");
- }
- int main()
- {
- freopen("cryptcow.in","r",stdin);
- freopen("cryptcow.out","w",stdout);
- read();
- work();
- return 0;
- }