Advertisement
a53

Dictionar

a53
May 22nd, 2020
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.51 KB | None | 0 0
  1. #include <fstream>
  2. #include <cstring>
  3. #include <vector>
  4. #include <queue>
  5. #define LL 251 /// Lungime linie
  6. #define N 61 /// Numar termeni
  7. using namespace std;
  8. char S[LL],Cuv[N][LL];
  9. int Grad[N],Rez[N],K,L;
  10. bool Viz[N];
  11. vector <int> X[N];
  12. struct compara
  13. {
  14. bool operator()(int x,int y)
  15. {
  16. return x>y;
  17. }
  18. };
  19. priority_queue <int,vector <int>,compara> Q;
  20.  
  21. void prelucrareCuvinte()
  22. {
  23. char *p=strtok(S," ");
  24. while(p)
  25. strcpy(Cuv[++K],p),p=strtok(NULL," ");
  26. }
  27.  
  28. int main()
  29. {
  30. ifstream f("dictionar.in");
  31. f.getline(S,LL);
  32. prelucrareCuvinte();
  33. int p=1;
  34. while(p<=K)
  35. {
  36. f.getline(S,LL);
  37. char *poi=strtok(S," ");
  38. while(poi)
  39. {
  40. for(int i=1;i<=K;++i)
  41. if(strcmp(poi,Cuv[i])==0&&p!=i)
  42. X[i].push_back(p),++Grad[p];
  43. poi=strtok(NULL," ");
  44. }
  45. ++p;
  46. }
  47. f.close();
  48. for(int i=1;i<=K;++i)
  49. if(!Grad[i])
  50. Q.push(i),Viz[i]=1;
  51. while(!Q.empty())
  52. {
  53. int curent=Q.top();
  54. Rez[++L]=curent;
  55. Q.pop();
  56. for(unsigned int i=0;i<X[curent].size();++i)
  57. {
  58. int nod=X[curent][i];
  59. --Grad[nod];
  60. if(!Grad[nod]&&!Viz[nod])
  61. Q.push(nod),Viz[nod]=1;
  62. }
  63. }
  64. ofstream g("dictionar.out");
  65. if(L==K)
  66. for(int i=1;i<=L;++i)
  67. g<<Cuv[Rez[i]]<<' ';
  68. else
  69. g<<"IMPOSIBIL";
  70. g.close();
  71. return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement