Guest User

Untitled

a guest
Feb 20th, 2018
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.09 KB | None | 0 0
  1. #include <fstream>
  2. #include <string>
  3. using namespace std;
  4. string origin,input;
  5. int needtime;
  6. int get = 0;
  7. bool hash[131071];
  8. int position[29];
  9. int hasher(string str)//EFL hash
  10. {
  11. unsigned int h=0,g,i;
  12. for(i=0;i<str.length();i++)
  13. {
  14. h=(h<<4)+str[i];
  15. g=h &0xf0000000l; //7 '0's;
  16. if(g) h^=g>>24;
  17. h&=~g;
  18. }
  19. return h%131071;
  20. }
  21.  
  22.  
  23. //to find the substring in original string
  24. int prune1(string str)
  25. {
  26. int start=0;
  27. int notfind = 0;
  28. string temp;
  29. for ( int i = 0 ; i<str.length() ;i++ )
  30. {
  31. if (str[i]=='C'||str[i]=='O'||str[i]=='W'||i==str.length()-1)
  32. {
  33.  
  34.  
  35. if (i==start){ start++; continue;}
  36. if (i==str.length()-1&&str[i]!='W'&&str[i]!='C'&&str[i]!='O')
  37. temp = str.substr(start,i-start+1);
  38.  
  39. else temp = str.substr(start,i-start);
  40. if (temp[0]=='B')
  41. notfind = origin.find(temp,position[26]);
  42. else if (temp[0]=='E')
  43. notfind = origin.find(temp,position[27]);
  44. else if (temp[0]=='D')
  45. notfind = origin.find(temp,position[28]);
  46. else
  47. notfind = origin.find(temp,position[temp[0]-'a']);
  48. start = i+1;
  49. if (notfind==-1)
  50. return 1;//not match!
  51.  
  52. }
  53. }
  54. return 0;
  55. }
  56. int prune3(string str)
  57. {
  58. int i;
  59. for (i = 0 ;i<str.length();i++)
  60. {
  61. if (str[i]=='C'||str[i]=='W'||str[i]=='O')
  62. break;
  63. }
  64. if (str[i]!='C')
  65. {
  66. return 1;
  67. }
  68.  
  69. int j;
  70. for (j = str.length() ;j>=0;j--)
  71. {
  72. if (str[j]=='C'||str[j]=='W'||str[j]=='O')
  73. break;
  74. }
  75. if (str[j]!='W')
  76. {
  77. return 1;
  78. }
  79. if (i>j&&str[i]=='C'&&str[j]=='W') return 1;
  80. return 0;
  81.  
  82. }
  83.  
  84.  
  85. void dfs(string str,int times)
  86. {
  87.  
  88. if (get) return;
  89. //prune 1
  90. if (prune1(str)) return;
  91. string temp1,temp2,temp3,temp4;
  92. for (int o = 0 ; o<str.length(); o++)
  93. if (str[o]=='O')
  94. for (int c=0; c<o;c++)
  95. if (str[c]=='C')
  96. for (int w=str.length()-1 ; w>o; w--)
  97. if (str[w]=='W')
  98. {
  99. if (c>0) temp1 = str.substr(0,c);
  100. else temp1 = "";
  101. if (w<str.length()-1) temp2 = str.substr(w+1,str.length()-1-w);
  102. else temp2 = "";
  103. if (o+1==w) temp3 = "";
  104. else temp3 = str.substr(o+1,w-o-1);
  105. if (c+1==o) temp4="";
  106. else temp4 = str.substr(c+1,o-c-1);
  107. string next = temp1 + temp3 + temp4 +temp2;//swap
  108.  
  109. if ( next==origin )
  110. {
  111. needtime=times ; get = 1 ;
  112. return;
  113. }//get
  114. else
  115. {
  116. //prun no.2:hash
  117.  
  118. int x = hasher(next);
  119. if (hash[x]) continue;
  120. hash[x] = true;
  121.  
  122. //prune no.3:1stC and last W
  123. if (prune3(next))
  124. {
  125. continue;
  126. }
  127.  
  128. dfs(next,times+1);
  129. }
  130. }
  131. }
  132. int main()
  133. {
  134. ifstream fin("cryptcow.in");
  135. ofstream fout("cryptcow.out");
  136. origin = "Begin the Escape execution at the Break of Dawn";
  137. getline(fin,input);
  138. if (input==origin) {fout<<1<<' '<<0<<endl;return 0;}
  139. if ((input.length()-47)%3 ){fout<<0<<' '<<0<<endl;return 0;}
  140. int count1[29] = {0};//B2,E1,D1,e7,g1,i2,n3,
  141. int count2[29] = {0};
  142. int nc=0,no=0,nw=0;
  143. for (int i = 0 ; i<input.length(); i++)
  144. {
  145. if (input[i]=='C'||input[i]=='O'||input[i]=='W')continue;
  146. else if (input[i]=='C')nc++;
  147. else if (input[i]=='O')no++;
  148. else if (input[i]=='W')nw++;
  149. else if (input[i]=='B') count1[26]++;
  150. else if (input[i]=='E') count1[27]++;
  151. else if (input[i]=='D') count1[28]++;
  152. else count1[input[i]-'a']++;
  153. }
  154. if (nc!=no||no!=nw||nc!=nw)
  155. {fout<<0<<' '<<0<<endl;return 0;}
  156. for (int i = 0 ; i<origin.length(); i++)
  157. {
  158. if (origin[i]=='C'||origin[i]=='O'||origin[i]=='W')continue;
  159. else if (origin[i]=='B') count2[26]++;
  160. else if (origin[i]=='E') count2[27]++;
  161. else if (origin[i]=='D') count2[28]++;
  162. else count2[origin[i]-'a']++;
  163. }
  164. for (int i = 0; i<29; i++)
  165. {
  166. if (count1[i]!=count2[i])
  167. {fout<<0<<' '<<0<<endl;return 0;}
  168. }
  169.  
  170. //precaculat the position of char in origin
  171. for (int i = origin.length()-1 ;i>=0 ;i--)
  172. {
  173. if (origin[i]=='C'||origin[i]=='O'||origin[i]=='W')continue;
  174. else if (origin[i]=='B') position[26] = i;
  175. else if (origin[i]=='E') position[27] = i;
  176. else if (origin[i]=='D') position[28] = i;
  177. else position[origin[i]-'a'] = i;
  178. }
  179. dfs(input,1);
  180.  
  181. fout<<get<<' '<<needtime<<endl;
  182. fin.close();
  183. fout.close();
  184. return 0;
  185. }
Add Comment
Please, Sign In to add comment