fork download
  1. #include <iostream>
  2. #include <cstdio>
  3. #include <vector>
  4. #include <string>
  5. #include <algorithm>
  6. using namespace std;
  7.  
  8. #define rep(i, n) reps(i, 0, n)
  9. #define reps(i, m, n) for (int i = m; i < int(n); ++i)
  10.  
  11.  
  12. const int N = 1<<20;
  13. #define DEBUG 0
  14.  
  15. int n;
  16. int g[N];
  17. string cipher;
  18.  
  19. string modify(int bits) {
  20. string str = cipher;
  21. rep(j, n) {
  22. if(bits & (1<<(n-1-j))) {
  23. if(str[j] == 'z')
  24. return "";
  25. str[j]++;
  26. }
  27. }
  28. return str;
  29. }
  30. string invmod(int bits) {
  31. string str = cipher;
  32. rep(j, n) {
  33. if(bits & (1<<(n-1-j))) {
  34. if(str[j] == 'z')
  35. return "";
  36. str[j]++;
  37. }
  38. }
  39. return str;
  40. }
  41.  
  42. bool check(string str) {
  43. reps(c, 'b', 'z'+1) {
  44. rep(i, str.size()) {
  45. if(str[i] == (char)c) {
  46. str[i] --;
  47. break;
  48. }
  49. }
  50. }
  51. return str == cipher;
  52. }
  53.  
  54. void proc(void) {
  55. rep(i, 1<<n) {
  56. string str=invmod(i);
  57. if(str=="") continue;
  58. if(check(str))g[i]=1;
  59. }
  60. }
  61.  
  62. int main()
  63. {
  64. string str;
  65. while(1) {
  66. cin>>str;
  67. if(str=="#") break;
  68. cipher = str;
  69. rep(i,N)g[i]=0;
  70. n = cipher.size();
  71. proc();
  72. int count = 0;
  73. vector<int> ss;
  74. rep(i, 1<<n) {
  75. count += g[i];
  76. if(g[i]){
  77. ss.push_back(i);
  78. }
  79. }
  80. sort(ss.begin(),ss.end());
  81. if(DEBUG) {
  82. if(count <= 10)
  83. rep(k, count) {
  84. cout << k << "-" << ss[k] << ":" << modify(ss[k]) << endl;
  85. }
  86. }
  87. cout << count << endl;
  88. if( count <= 10) {
  89. rep(k, count) {
  90. cout << modify(ss[k]) << endl;
  91. }
  92. }
  93. else {
  94. rep(k,5) {
  95. cout << modify(ss[k]) << endl;
  96. }
  97. rep(k,5) {
  98. cout << modify(ss[ss.size()-5+k]) << endl;
  99. }
  100. }
  101. }
  102. }
  103.  
Success #stdin #stdout 2.26s 7536KB
stdin
enw
abc
abcdefghijklmnopqrst
z
#
stdout
1
fox
5
acc
acd
bbd
bcc
bcd
17711
acceeggiikkmmooqqssu
acceeggiikkmmooqqstt
acceeggiikkmmooqqstu
acceeggiikkmmooqrrtt
acceeggiikkmmooqrrtu
bcdefghijklmnopqrrtt
bcdefghijklmnopqrrtu
bcdefghijklmnopqrssu
bcdefghijklmnopqrstt
bcdefghijklmnopqrstu
0