fork download
  1. // iostream is too mainstream
  2. #include <cstdio>
  3. // bitch please
  4. #include <iostream>
  5. #include <algorithm>
  6. #include <cstdlib>
  7. #include <vector>
  8. #include <set>
  9. #include <map>
  10. #include <queue>
  11. #include <stack>
  12. #include <list>
  13. #include <cmath>
  14. #include <iomanip>
  15. #define dibs reserve
  16. #define OVER9000 123456789012345LL
  17. #define ALL_THE(CAKE,LIE) for(auto LIE =CAKE.begin(); LIE != CAKE.end(); LIE++)
  18. #define tisic 47
  19. #define soclose 1e-10
  20. #define chocolate win
  21. // so much chocolate
  22. #define patkan 9
  23. #define ff first
  24. #define ss second
  25. #define abs(x) ((x < 0)?-(x):x)
  26. #define uint unsigned int
  27. using namespace std;
  28. // mylittledoge
  29.  
  30. vector<int> ans;
  31. bool sol;
  32. vector<int> Vi;
  33. vector< vector<bool> > B;
  34. vector< vector<bool> > B2;
  35.  
  36. void bt(int k) {
  37. if(k == 9) {
  38. sol =true;
  39. return;}
  40. int mx =Vi.size();
  41. if(k%3 == 0 && k > 0) mx =ans[k-3];
  42. else if(k > 0) mx =ans[k-1];
  43. for(int q =0; q < mx; q++) {
  44. ans[k] =q;
  45. bool ok =true;
  46. for(int i =0; i < k; i++) ok =ok&B[ans[k]][ans[i]];
  47. if(!ok) continue;
  48. for(int i =k-k%3; i < k; i++) ok =ok&B2[ans[k]][ans[i]];
  49. if(!ok) continue;
  50. bt(k+1);
  51. if(sol) return;}
  52. }
  53.  
  54. int main() {
  55. cin.sync_with_stdio(0);
  56. cin.tie(0);
  57. srand(time(0));
  58. string s,P;
  59. while(P.size() < 10000000) {
  60. cin >> s;
  61. if(s.empty()) break;
  62. P +=s;}
  63.  
  64. map<string,int> perm;
  65. vector<int> Perm(9);
  66. for(int i =0; i < 9; i++) Perm[i] =i+1;
  67. vector<string> Pi(362880);
  68. while(true) {
  69. int a =perm.size();
  70. if(Perm.size() > 362880) break;
  71. s.clear();
  72. for(int i =0; i < 9; i++) s +=('0'+Perm[i]);
  73. perm[s] =a;
  74. Pi[a] =s;
  75. if(!next_permutation(Perm.begin(),Perm.end())) break;}
  76. vector<int> V(362880,20000000);
  77. for(int i =0; i < (int)P.length()-9; i++) if(perm.find(P.substr(i,9)) != perm.end()) {
  78. if(V[perm[P.substr(i,9)]] == 20000000) Vi.push_back(perm[P.substr(i,9)]);
  79. V[perm[P.substr(i,9)]] =min(V[perm[P.substr(i,9)]],i+1);}
  80.  
  81. B.resize(Vi.size(),vector<bool>(Vi.size(),true));
  82. for(uint i =0; i < Vi.size(); i++) for(uint j =i; j < Vi.size(); j++)
  83. for(int k =0; k < 9; k++) if(Pi[Vi[j]][k] == Pi[Vi[i]][k]) {
  84. B[i][j] =B[j][i] =false;
  85. break;}
  86. B2.resize(Vi.size(),vector<bool>(Vi.size(),true));
  87. for(uint i =0; i < Vi.size(); i++) for(uint j =i; j < Vi.size(); j++)
  88. for(int a =0; a < 3; a++) for(int b =0; b < 3; b++) for(int c =0; c < 3; c++)
  89. if(Pi[Vi[i]][3*a+b] == Pi[Vi[j]][3*a+c]) B2[i][j] =B2[j][i] =false;
  90. cout << Vi.size() << "\n";
  91.  
  92. ans.resize(9,0);
  93. sol =false;
  94. cerr << "start\n";
  95. bt(0);
  96. cout << sol << endl;
  97. for(int i =0; i < 9; i++) cout << Pi[Vi[ans[i]]] << " " << V[Vi[ans[i]]] << "\n";
  98. return 0;}
  99.  
  100. // look at my code
  101. // my code is amazing
  102.  
Runtime error #stdin #stdout #stderr 0.6s 31944KB
stdin
Standard input is empty
stdout
0
0
stderr
start