Advertisement
Guest User

Untitled

a guest
Feb 18th, 2020
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.66 KB | None | 0 0
  1. #include <iostream>
  2. #include <fstream>
  3. #include <string>
  4. #include <algorithm>
  5. #include <vector>
  6. using namespace std;
  7.  
  8. bool areAnagrams(string a, string b)
  9. {
  10.     int lengthA = a.length();
  11.     int lengthB = b.length();
  12.     int j = 0;
  13.  
  14.     if(lengthA != lengthB)
  15.         return false;
  16.  
  17.     sort(a.begin(),a.end());
  18.     sort(b.begin(),b.end());
  19.  
  20.     for(int j = 0; j < lengthA; j++)
  21.         if(a[j] != b[j])
  22.             return false;
  23.     return true;
  24. }
  25. bool subcheck(string check, string a)
  26. {
  27.     int checkCount = 0;
  28.     for(int i = 0; i < a.length() - 2; i++)
  29.     {
  30.         if(a.substr(i,3) == check)
  31.             checkCount++;
  32.     }
  33.     if(checkCount > 1)
  34.         return true;
  35. }
  36.  
  37. int main()
  38. {
  39.     ifstream data("hasla.txt");
  40.     ofstream result;
  41.     result.open("wynik.txt", ios::out);
  42.     vector<string> result42 ;
  43.     string a, b;
  44.     string combos[6] = {"CBA","CAB","BAC","BCA","ACB","ABC"};
  45.     int i = 0, anagramCount = 0, checkTrueA = 0, checkTrueB = 0;
  46.     for(i = 0; i< 1000; i++)
  47.     {
  48.         data >> a >> b;
  49.         if(areAnagrams(a,b) == true)
  50.             anagramCount ++;
  51.         for(int j = 0; j < 6; j++)
  52.         {
  53.             if(subcheck(combos[j],a))
  54.                 checkTrueA ++;
  55.             if(subcheck(combos[j],b))
  56.                 checkTrueB ++;
  57.         }
  58.         if(checkTrueA > 0)
  59.             result42.push_back(a);
  60.  
  61.         if(checkTrueB > 0)
  62.             result42.push_back(b);
  63.     }
  64.     cout << anagramCount;
  65.     result << "4.1" << endl << "ilosc anagramow: " << anagramCount << endl;
  66.     result.close();
  67.     for(i = 0; i< result42.size(); i++)
  68.     {
  69.         cout << result42[i]<< endl;
  70.     }
  71.     return 0;
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement