Advertisement
MiinaMagdy

454 - Anagrams

Sep 6th, 2022
678
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.08 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. #define ll long long
  6. #define endl '\n'
  7. #define sz(x) int(x.size())
  8. #define all(x) x.begin(), x.end()
  9.  
  10. bool is_anagram(string s, string t) {
  11.     char freq[300]{};
  12.     for (int i = 0; i < sz(s); i++) if (s[i] != ' ') freq[s[i]]++;
  13.     for (int i = 0; i < sz(t); i++) if (t[i] != ' ') freq[t[i]]--;
  14.     for (int i = 0; i < 300; i++) if (freq[i]) return false;
  15.     return true;
  16. }
  17.  
  18. int main() {
  19.     ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
  20.     int testcases;
  21.     cin >> testcases;
  22.     cin.ignore();
  23.     cin.ignore();
  24.     for (int test = 1; test <= testcases; test++) {
  25.         if (test > 1) cout << endl;
  26.         string lines[105];
  27.         int sz = 0;
  28.         while (getline(cin, lines[sz]), lines[sz++] != "");
  29.         vector<string> ans;
  30.         for (int i = 0; i < sz; i++) {
  31.             for (int j = i + 1; j < sz; j++) {
  32.                 if (is_anagram(lines[i], lines[j])) {
  33.                     if (lines[i] < lines[j]) ans.push_back(lines[i] + " = " + lines[j]);
  34.                     else ans.push_back(lines[j] + " = " + lines[i]);
  35.                 }
  36.             }
  37.         }
  38.         sort(all(ans));
  39.         for (auto& I : ans) {
  40.             cout << I << endl;
  41.         }
  42.     }
  43. }
  44.  
Tags: UVA CP3
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement