Advertisement
a53

anagrame_pfv

a53
Oct 7th, 2019
197
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <algorithm>
  4. #define NR 12
  5. using namespace std;
  6. int x[NR],L,n,k;
  7. char s[NR-1],c[NR],v[NR];
  8.  
  9. void init(int k)
  10. {
  11. x[k]=0;
  12. }
  13.  
  14. bool succesor(int k)
  15. {
  16. if(x[k]<n)
  17. return true;
  18. else
  19. return false;
  20. }
  21.  
  22. bool continuare(int k)
  23. {
  24. for(int i=1;i<=k-1;++i) /// Valorile sa fie distincte
  25. if(x[k]==x[i])
  26. return false;
  27. return true;
  28. }
  29.  
  30. bool solutie(int k)
  31. {
  32. if(k==n+1)
  33. return true;
  34. else
  35. return false;
  36. }
  37.  
  38. void afisare()
  39. {
  40. int j=0;
  41. for(int i=1;i<=L;++i)
  42. if(v[i])
  43. cout<<v[i];
  44. else
  45. cout<<c[x[++j]-1];
  46. cout<<'\n';
  47. }
  48.  
  49. void backtracking() /// Generam permutarile multimii {1,2,...,n}
  50. {
  51. int k=1;
  52. init(k);
  53. while(k)
  54. if(solutie(k))
  55. {
  56. afisare();
  57. --k;
  58. }
  59. else
  60. if(succesor(k))
  61. {
  62. ++x[k];
  63. if(continuare(k))
  64. ++k;
  65. }
  66. else
  67. {
  68. init(k);
  69. --k;
  70. }
  71. }
  72.  
  73. int main()
  74. {
  75. cin>>s;
  76. L=strlen(s);
  77. n=0;
  78. for(int i=0;i<L;++i)
  79. if(strchr("aeiou",s[i]))
  80. v[i+1]=s[i];
  81. else
  82. c[n++]=s[i];
  83. sort(c,c+n);
  84. backtracking();
  85. return 0;
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement