Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <fstream>
- #include <cstring>
- #include <vector>
- #include <queue>
- #define LL 251 /// Lungime linie
- #define N 61 /// Numar termeni
- using namespace std;
- char S[LL],Cuv[N][LL];
- int Grad[N],Rez[N],K,L;
- bool Viz[N];
- vector <int> X[N];
- struct compara
- {
- bool operator()(int x,int y)
- {
- return x>y;
- }
- };
- priority_queue <int,vector <int>,compara> Q;
- void prelucrareCuvinte()
- {
- char *p=strtok(S," ");
- while(p)
- strcpy(Cuv[++K],p),p=strtok(NULL," ");
- }
- int main()
- {
- ifstream f("dictionar.in");
- f.getline(S,LL);
- prelucrareCuvinte();
- int p=1;
- while(p<=K)
- {
- f.getline(S,LL);
- char *poi=strtok(S," ");
- while(poi)
- {
- for(int i=1;i<=K;++i)
- if(strcmp(poi,Cuv[i])==0&&p!=i)
- X[i].push_back(p),++Grad[p];
- poi=strtok(NULL," ");
- }
- ++p;
- }
- f.close();
- for(int i=1;i<=K;++i)
- if(!Grad[i])
- Q.push(i),Viz[i]=1;
- while(!Q.empty())
- {
- int curent=Q.top();
- Rez[++L]=curent;
- Q.pop();
- for(unsigned int i=0;i<X[curent].size();++i)
- {
- int nod=X[curent][i];
- --Grad[nod];
- if(!Grad[nod]&&!Viz[nod])
- Q.push(nod),Viz[nod]=1;
- }
- }
- ofstream g("dictionar.out");
- if(L==K)
- for(int i=1;i<=L;++i)
- g<<Cuv[Rez[i]]<<' ';
- else
- g<<"IMPOSIBIL";
- g.close();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement