nicuvlad76

Untitled

Oct 29th, 2022
966
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.88 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. ///
  4. ifstream fin("lant2.in");
  5. ofstream fout("lant2.out");
  6. const short Cuvmax=35;
  7. const short Nrdist=160;
  8. const int Pmax=1005;
  9.  
  10. char s[Pmax], a[Nrdist][Cuvmax], *p, sep[]=" ,:;.!?-";
  11. int k,n, grad[Nrdist],gradcpy[Nrdist], S[Nrdist];
  12. vector<short> L[Nrdist+105];
  13. queue<int> Q;
  14. void Citire()
  15. {
  16.     fin>>k;fin.get();
  17.     n=0;
  18.     while(fin.getline(s,Pmax))
  19.     {
  20.         p=strtok(s,sep);
  21.         while(p)
  22.         {
  23.           bool ok=false;
  24.           for(int i=1;i<=n && !ok; ++i)
  25.             if(strcmp(p,a[i])==0) ok=true;
  26.           if(!ok)
  27.           {
  28.               ++n;
  29.               strcpy(a[n],p);
  30.           }
  31.           p=strtok(0,sep);
  32.         }
  33.     }
  34. }
  35. int Levenshtein(char b[], char c[])
  36. {
  37.     int dp[Cuvmax][Cuvmax],X, Y;
  38.     X=strlen(b);Y=strlen(c);
  39.     ///initializari
  40.     for(int i=0;i<=X;i++)dp[i][0]=i;
  41.     for(int i=0;i<=Y;i++)dp[0][i]=i;
  42.  
  43.     for(int i=0;i<X;i++)
  44.         for(int j=0;j<Y;j++)
  45.     {
  46.         dp[i+1][j+1]=min(1+dp[i][j+1], 1+dp[i+1][j]);
  47.         if(b[i]==c[j] && dp[i+1][j+1]>dp[i][j]) dp[i+1][j+1]=dp[i][j];
  48.     }
  49.     return dp[X][Y];
  50. }
  51. void Solutie()
  52. {
  53.     for(int i=1;i<n;i++)
  54.         for(int j=i+1;j<=n;j++)
  55.           if(Levenshtein(a[i], a[j])<=k)
  56.     {
  57.         ++grad[j];
  58.         ++gradcpy[i];
  59.         L[i].push_back(j);
  60.     }
  61.     ///bfs
  62.     S[1]=1;
  63.     for(int i=1;i<=n;i++)
  64.         if(!grad[i])Q.push(i);
  65.     while(!Q.empty())
  66.     {
  67.         int varf=Q.front();
  68.         Q.pop();
  69.         for(unsigned int j=0;j<L[varf].size();j++)
  70.         {
  71.             int w=L[varf][j];
  72.             S[w]+=S[varf];
  73.             --grad[w];
  74.             if(!grad[w]) Q.push(w);
  75.         }
  76.     }
  77.     ///numarare sol
  78.     long long sol=0;
  79.     for(int i=1;i<=n;i++)
  80.         if(!gradcpy[i])
  81.          sol+=S[i];
  82.     fout<<sol;
  83.  
  84. }
  85. int main()
  86. {
  87.   Citire();
  88.   Solutie();
  89.   return 0;
  90. }
  91.  
Advertisement
Add Comment
Please, Sign In to add comment