Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <string>
- using namespace std;
- string origin,input;
- int needtime;
- int get = 0;
- bool hash[131071];
- int position[29];
- int hasher(string str)//EFL hash
- {
- unsigned int h=0,g,i;
- for(i=0;i<str.length();i++)
- {
- h=(h<<4)+str[i];
- g=h &0xf0000000l; //7 '0's;
- if(g) h^=g>>24;
- h&=~g;
- }
- return h%131071;
- }
- //to find the substring in original string
- int prune1(string str)
- {
- int start=0;
- int notfind = 0;
- string temp;
- for ( int i = 0 ; i<str.length() ;i++ )
- {
- if (str[i]=='C'||str[i]=='O'||str[i]=='W'||i==str.length()-1)
- {
- if (i==start){ start++; continue;}
- if (i==str.length()-1&&str[i]!='W'&&str[i]!='C'&&str[i]!='O')
- temp = str.substr(start,i-start+1);
- else temp = str.substr(start,i-start);
- if (temp[0]=='B')
- notfind = origin.find(temp,position[26]);
- else if (temp[0]=='E')
- notfind = origin.find(temp,position[27]);
- else if (temp[0]=='D')
- notfind = origin.find(temp,position[28]);
- else
- notfind = origin.find(temp,position[temp[0]-'a']);
- start = i+1;
- if (notfind==-1)
- return 1;//not match!
- }
- }
- return 0;
- }
- int prune3(string str)
- {
- int i;
- for (i = 0 ;i<str.length();i++)
- {
- if (str[i]=='C'||str[i]=='W'||str[i]=='O')
- break;
- }
- if (str[i]!='C')
- {
- return 1;
- }
- int j;
- for (j = str.length() ;j>=0;j--)
- {
- if (str[j]=='C'||str[j]=='W'||str[j]=='O')
- break;
- }
- if (str[j]!='W')
- {
- return 1;
- }
- if (i>j&&str[i]=='C'&&str[j]=='W') return 1;
- return 0;
- }
- void dfs(string str,int times)
- {
- if (get) return;
- //prune 1
- if (prune1(str)) return;
- string temp1,temp2,temp3,temp4;
- for (int o = 0 ; o<str.length(); o++)
- if (str[o]=='O')
- for (int c=0; c<o;c++)
- if (str[c]=='C')
- for (int w=str.length()-1 ; w>o; w--)
- if (str[w]=='W')
- {
- if (c>0) temp1 = str.substr(0,c);
- else temp1 = "";
- if (w<str.length()-1) temp2 = str.substr(w+1,str.length()-1-w);
- else temp2 = "";
- if (o+1==w) temp3 = "";
- else temp3 = str.substr(o+1,w-o-1);
- if (c+1==o) temp4="";
- else temp4 = str.substr(c+1,o-c-1);
- string next = temp1 + temp3 + temp4 +temp2;//swap
- if ( next==origin )
- {
- needtime=times ; get = 1 ;
- return;
- }//get
- else
- {
- //prun no.2:hash
- int x = hasher(next);
- if (hash[x]) continue;
- hash[x] = true;
- //prune no.3:1stC and last W
- if (prune3(next))
- {
- continue;
- }
- dfs(next,times+1);
- }
- }
- }
- int main()
- {
- ifstream fin("cryptcow.in");
- ofstream fout("cryptcow.out");
- origin = "Begin the Escape execution at the Break of Dawn";
- getline(fin,input);
- if (input==origin) {fout<<1<<' '<<0<<endl;return 0;}
- if ((input.length()-47)%3 ){fout<<0<<' '<<0<<endl;return 0;}
- int count1[29] = {0};//B2,E1,D1,e7,g1,i2,n3,
- int count2[29] = {0};
- int nc=0,no=0,nw=0;
- for (int i = 0 ; i<input.length(); i++)
- {
- if (input[i]=='C'||input[i]=='O'||input[i]=='W')continue;
- else if (input[i]=='C')nc++;
- else if (input[i]=='O')no++;
- else if (input[i]=='W')nw++;
- else if (input[i]=='B') count1[26]++;
- else if (input[i]=='E') count1[27]++;
- else if (input[i]=='D') count1[28]++;
- else count1[input[i]-'a']++;
- }
- if (nc!=no||no!=nw||nc!=nw)
- {fout<<0<<' '<<0<<endl;return 0;}
- for (int i = 0 ; i<origin.length(); i++)
- {
- if (origin[i]=='C'||origin[i]=='O'||origin[i]=='W')continue;
- else if (origin[i]=='B') count2[26]++;
- else if (origin[i]=='E') count2[27]++;
- else if (origin[i]=='D') count2[28]++;
- else count2[origin[i]-'a']++;
- }
- for (int i = 0; i<29; i++)
- {
- if (count1[i]!=count2[i])
- {fout<<0<<' '<<0<<endl;return 0;}
- }
- //precaculat the position of char in origin
- for (int i = origin.length()-1 ;i>=0 ;i--)
- {
- if (origin[i]=='C'||origin[i]=='O'||origin[i]=='W')continue;
- else if (origin[i]=='B') position[26] = i;
- else if (origin[i]=='E') position[27] = i;
- else if (origin[i]=='D') position[28] = i;
- else position[origin[i]-'a'] = i;
- }
- dfs(input,1);
- fout<<get<<' '<<needtime<<endl;
- fin.close();
- fout.close();
- return 0;
- }
Add Comment
Please, Sign In to add comment