Advertisement
a53

scrabble_NU_AJUNGE_LA_SOLUTIE

a53
Nov 23rd, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. #include <fstream>
  2. #include <cstring>
  3. using namespace std;
  4. ifstream f("scrabble.in");
  5. ofstream g("scrabble.out");
  6. int n,x[20],folosit[20];
  7. char cuv[20][3],voc[]="AEIOU";
  8. char s[20],smax[20];
  9.  
  10. int OK(int k)
  11. {
  12. unsigned L=strlen(s);
  13. if((strchr(voc,cuv[k][0])&&strchr(voc,s[L-1]))||
  14. (!strchr(voc,cuv[k][0])&&!strchr(voc,s[L-1]))||
  15. (cuv[k][0]==s[L-1]))
  16. return 0;
  17. return 1;
  18. }
  19.  
  20. void backr(int k)
  21. {
  22. if(k>n)
  23. {
  24. s[0]='\0';
  25. for(int i=1;i<=n;++i)
  26. if(OK(x[i]))
  27. strcat(s,cuv[x[i]]);
  28. g<<"S= "<<s<<' '<<endl;
  29. if(strcmp(s,smax)>=0)
  30. strcpy(smax,s);
  31. }
  32. else
  33. for(int i=1;i<=n;++i)
  34. {
  35. x[k]=i;
  36. if(!folosit[x[k]]) /// Daca elementul curent nu e deja folosit
  37. {
  38. folosit[x[k]]=1; /// Marcheaza-l ca folosit
  39. backr(k+1); /// Genereaza restul permutarii
  40. folosit[x[k]]=0; /// Demarcheaza elementul
  41. }
  42. }
  43. }
  44.  
  45. int main()
  46. {
  47. f>>n;
  48. f.get();
  49. for(int i=1;i<=n;++i)
  50. f>>cuv[i];
  51. for(int i=1;i<n;++i)
  52. for(int j=i+1;j<=n;++j)
  53. if(strcmp(cuv[i],cuv[j])>0)
  54. swap(cuv[i],cuv[j]);
  55. backr(1);
  56. g<<smax;
  57. return 0;
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement