fork(5) download
  1. #include <iostream>
  2. #include <set>
  3. #include <algorithm>
  4. #include <iterator>
  5. #include <vector>
  6. using namespace std;
  7. string s1[6],s2[6];
  8. string s1sol(5,0),s2sol(5,0);
  9. int in,k;
  10. void bt(int depth)
  11. {
  12.  
  13. if(depth == 5)
  14. {
  15. ++in;
  16. s1sol[5] = 0;
  17. if(in == k)
  18. cout<<s1sol<<endl;
  19. return;
  20.  
  21. }
  22. for(int i=0,j=0;i<6 && j<6;++i,++j)
  23. {
  24. s1sol[depth] = s1[i][depth];
  25. s2sol[depth] = s2[j][depth];
  26.  
  27. if(s1sol[depth]!=s2sol[depth])
  28. {
  29. if(s1sol[depth]<s2sol[depth])
  30. {
  31. int tmp=i;
  32. while(i<6 && s1[i][depth]!=s2sol[depth])
  33. ++i;
  34. if(i == 6)
  35. {
  36. i=tmp;
  37. continue;
  38.  
  39. }
  40. else
  41. s1sol[depth] = s2sol[depth];
  42. }
  43. else
  44. {
  45. int tmp = j;
  46. while(j<6&&s2[j][depth]!=s1sol[depth])
  47. ++j;
  48. if(j == 6)
  49. {
  50. j=tmp;
  51. continue;
  52. }
  53. else
  54. s2sol[depth] = s1sol[depth];
  55. }
  56. }
  57. bt(depth+1);
  58.  
  59.  
  60. }
  61.  
  62. }
  63.  
  64. int main()
  65. {
  66. int t;
  67. cin>>t;
  68. while(t--)
  69. {
  70. in =0;
  71. cin>>k;
  72. for(int i=0;i<6;++i)
  73. {
  74. s1[i].clear();
  75. cin>>s1[i];
  76. }
  77. for(int i=0;i<6;++i)
  78. {
  79. s2[i].clear();
  80. cin>>s2[i];
  81. }
  82. for(int i=0;i<5;++i)
  83. {
  84. for(int j=0;j<6;++j)
  85. for(int k=0;k<5;++k)
  86. {
  87. if(s1[k][i]>s1[k+1][i])
  88. {
  89. char temp = s1[k][i];
  90. s1[k][i] = s1[k+1][i];
  91. s1[k+1][i] = temp;
  92. }
  93. if(s2[k][i]>s2[k+1][i])
  94. {
  95. char temp = s2[k][i];
  96. s2[k][i] = s2[k+1][i];
  97. s2[k+1][i] = temp;
  98.  
  99. }
  100.  
  101. }
  102.  
  103. }
  104.  
  105. bt(0);
  106. if(in<k)
  107. cout<<"NO"<<endl;
  108. }
  109. }
  110.  
Time limit exceeded #stdin #stdout 5s 3476KB
stdin
Standard input is empty
stdout
Standard output is empty