fork(3) download
  1. #include <iostream>
  2. #include <stdio.h>
  3. #include <vector>
  4. #include <string>
  5. #include <algorithm>
  6. #include <bitset>
  7. using namespace std;
  8.  
  9. struct word{
  10. string s;
  11. int clas;
  12. };
  13. bool operator<(const word &a, const word &b){
  14. return a.clas<b.clas;
  15. }
  16. bool operator==(const word &a, const word &b){
  17. return a.clas==b.clas;
  18. }
  19.  
  20. typedef vector <word> vw;
  21. typedef vector <char> vc;
  22.  
  23. word dic[1010];
  24. int n;
  25. bool found;
  26. vc cryptab(26);
  27.  
  28. bool apps (vc &tab2, word &liner, word &dicter){
  29. int nn=liner.s.size();
  30. for (int i=0; i<nn; ++i){
  31. if (tab2[liner.s[i]-'a']==0){
  32. tab2[liner.s[i]-'a']=dicter.s[i];
  33. }
  34. else if(tab2[liner.s[i]-'a']!=dicter.s[i]){
  35. return false;
  36. }
  37. }
  38. return true;
  39. }
  40.  
  41. void decryp (vw &line, int ci, int lsize, vc table ){
  42. if (found) return;
  43. vc tab2(26);
  44.  
  45. int i= lower_bound(dic, dic+n, line[ci]) - dic;
  46. while (line[ci]==dic[i]){
  47. tab2=table;
  48. if (apps(tab2, line[ci], dic[i])){
  49. if (ci==lsize-1){
  50. cryptab=tab2;
  51. found=true;
  52. return;
  53. }
  54. else{
  55. decryp(line, ci+1, lsize, tab2);
  56. }
  57. }
  58.  
  59. ++i;
  60. }
  61. }
  62.  
  63. int main(){
  64. bitset <'z'+5> rep;
  65. int ssize;
  66. cin>>n;
  67. for (int i=0; i<n; ++i){
  68. cin>>dic[i].s;
  69. dic[i].clas=dic[i].s.size();
  70. rep.reset();
  71. ssize=dic[i].clas;
  72. dic[i].clas*=100;
  73. for (int j=0; j<ssize; ++j){
  74. if (rep[dic[i].s[j]]){
  75. ++dic[i].clas;
  76. }
  77. else{
  78. rep.set(dic[i].s[j]);
  79. }
  80. }
  81. }
  82.  
  83. sort(dic, dic+n);
  84.  
  85. word temp;
  86. while (cin>>temp.s){
  87. temp.clas=temp.s.size();
  88. rep.reset();
  89. ssize=temp.clas;
  90. temp.clas*=100;
  91. for (int i=0; i<ssize; ++i){
  92. if (rep[temp.s[i]]){
  93. ++temp.clas;
  94. }
  95. else{
  96. rep[temp.s[i]]=1;
  97. }
  98. }
  99.  
  100.  
  101. vw line;
  102. line.push_back(temp);
  103. while (true){
  104. while (cin.peek()==' ') getchar();
  105. if (cin.peek()=='\n'||cin.peek()==EOF) break;
  106. cin>>temp.s;
  107. temp.clas=temp.s.size();
  108. rep.reset();
  109. ssize=temp.clas;
  110. temp.clas*=100;
  111. for (int i=0; i<ssize; ++i){
  112. if (rep[temp.s[i]]){
  113. ++temp.clas;
  114. }
  115. else{
  116. rep[temp.s[i]]=1;
  117. }
  118. }
  119. line.push_back(temp);
  120. }
  121.  
  122. int lsize=line.size();
  123. vc table(26);
  124. fill(table.begin(), table.end(), 0);
  125. found=false;
  126. decryp (line, 0, lsize, table );
  127. if (found){
  128. for (int i=0; i<lsize; ++i){
  129. int ss=line[i].s.size();
  130. for (int j=0; j<ss; ++j){
  131. printf("%c", cryptab[line[i].s[j]-'a']);
  132. }
  133. if (i<lsize-1) printf(" ");
  134. }
  135. printf("\n");
  136. }
  137. else {
  138. for (int i=0; i<lsize; ++i){
  139. int ss=line[i].s.size();
  140. for (int j=0; j<ss; ++j){
  141. printf("*");
  142. }
  143. if (i<lsize-1) printf(" ");
  144. }
  145. printf("\n");
  146. }
  147. }
  148. return 0;
  149. }
Success #stdin #stdout 0s 3452KB
stdin
6
and
dick
jane
puff
spot
yertle
bjvg xsb hxsn xsb qymm xsb rqat xsb pnetfn
xxxx yyy zzzz www yyyy aaa bbbb ccc dddddd
stdout
dick and jane and puff and spot and yertle
**** *** **** *** **** *** **** *** ******