Advertisement
J00ker

Untitled

Oct 9th, 2015
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.98 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <cstring>
  4. #include <algorithm>
  5.  
  6. using namespace std;
  7.  
  8. int st[100], n;
  9. char S[9];
  10.  
  11. void Init(int k)
  12. {
  13.     st[k] = 0;
  14. }
  15.  
  16. int Succesor(int k)
  17. {
  18.     if(st[k] < n)
  19.     {
  20.         st[k]++;
  21.         return 1;
  22.     }
  23.     return 0;
  24. }
  25.  
  26. int Valid(int k)
  27. {
  28.     for(int i = 1; i <  k; i++)
  29.         if(st[k] == st[i])
  30.             return 0;
  31.     return 1;
  32. }
  33.  
  34. int Solutie(int k)
  35. {
  36.     if(k == n+1)
  37.         return 1;
  38.     return 0;
  39. }
  40.  
  41.  
  42. ofstream fout("anagrame1.out");
  43. void Tipar()
  44. {
  45.     for(int i = 1; i <= n; i++)
  46.         fout << S[st[i]-1];
  47.     fout << "\n";
  48. }
  49.  
  50. void backtrack(int k)
  51. {
  52.     if(Solutie(k))
  53.         Tipar();
  54.     else
  55.     {
  56.         Init(k);
  57.         while(Succesor(k))
  58.             if(Valid(k))
  59.                 backtrack(k+1);
  60.     }
  61. }
  62.  
  63. int main()
  64. {
  65.     ifstream fin("anagrame1.in");
  66.     fin.getline(S, 9);
  67.     n = strlen(S);
  68.     sort(S, S+n);
  69.     backtrack(1);
  70.     fin.close();
  71.     return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement